Date conversion handling YYYY-MM-DD HH:MM:SS.SSS - datetime

My source is a file and loading into SQL Server table. I'm working on a scenario where i have to convert a string '2019-04-02T21:24:00.065' to informatica datetime format.
I tried below expression but some times its failing due to we are not receiving milliseconds from our source file in few occasions.
IIF(NOT ISNULL(DATEFIELD),TO_DATE(SUBSTR (DATEFIELD, 0, 10) || ' ' || SUBSTR(DATEFIELD, 12, 12), 'YYYY-MM-DD HH24.MI.SS.US'),NULL)
I'm looking for a permanent fix to handle all types of datetime formats regardless of what we receive in the file.

Well... I'm sorry to say, but there is no magic component that will recognize all possible date and time formats (including e.g. verbal in swahili).
You will need to detect the format for yourself. You can use a DECODE function, like e.g.:
DECODE(True,
IS_DATE(your_input_port, 'DD/MM/YYYY'), TO_DATE(your_input_port, 'DD/MM/YYYY'),
...)

If you are completely sure that only seconds/milliseconds are the missing part, you can check for length, if less than 12, use RPAD to the second part of your SUBSTR with missing format, or you can use decode as suggested by #maciejg and write code for all possible date formats.

Thanks for your inputs guys. Since we are not sure which date format we are receiving , we decided to go with a simple fix for this. I have changed the target field to varchar and simply replacing the T with ' ' value since this a staging mapping.
Again thanks for your time and inputs.

Related

Format Date via Parameter

I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)

How can I convert timestamps to dates in SQLite?

How can I convert timestamps to dates in SQLite?
The following code only produces a Timestamp column and a Date columns with NULL values. The the SQL code needs to convert from a "08/28/2020 8:00" format.
SQL CODE:
'''Select Timestamp, strftime('%m-%d-%Y', Timestamp) as Date
FROM Room_Data'''
The SQLite documentation is pretty clear, but I can't seem to get the desired result.
The strftime is meant to format a date, rather than perform conversion.
In the meantime you could try something like that to gather the pieces:
SELECT Timestamp,
SUBSTR(c,7,4) || '-' || SUBSTR(Timestamp,1,2) || '-' || SUBSTR(Timestamp,4,2) as Date
FROM Room_Data
Since SQlite doesn't really have the concept of a date, unlike other DBMS, the best choice would be to convert your dates to integer, either as Unix timestamps or in string form (YYYY-MM-DD) but storing dates as integer like 20201010 would be acceptable too.
NB: be careful with names like Timestamp or Date, they are reserved keywords in many programming languages and DBMSes.
The original code won't work in Windows 10 for some reason. Trying this from a Linux distro (Kubuntu, in this case) seems to resolve the issue. In Windows, the date needs to be converted to a '2020-01-01' format to actually work.
SQLite is amazing, but not sure why functionality changes for Windows 10. Feel free to comment if you know more about the differences.

MSACCESS Form VBA Open Where Clause - US v AU Date Formatting

Morning all,
This should be a simple fix and I have searched around and found things that appear they should work, but they don't seem to though...
So - it's a simple "Open this form and show only records from This date" function and, you guessed it, good ol' US of A date formatting is the problem and no matter which way I try it nothing is solving the problem...
So - here's the form code...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "# " & NEWDATE & " #"
to find NEWDATE...
I strip the date components from a couple of combo boxes (CBOYear & CBOMonth) and a field that represents the day of the month (data will be in the format "1st" or "2nd" etc...)
I concatenate the individual components...
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
A msgbox NEWDATE popup results in the correct data (ie 1/3/18)
But when I run the code it either selects everything or nothing... the only time the function works is when the day field is 13 or higher (ie 13/3/18) - thus it can determine the correct format to work with.
I'm sure the correct answer is already here somewhere - the correct question however may be something I have not thought to search for.
I have tried to use DATEVALUE() before and within the form open code. I have tried to hard code the US format to see if that works...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "#3/1/18#"
No good... shows all records - same if I hard code for the AU format #1/3/18#
My PC is set up for AU format dd/mm/yy
So... Which function do I need to use to convert the AU Date string into US Date string prior to calling the Openform, so the correct records open? OR is there a better way to write the WHERE clause in the Docmd line to achieve this result? - Or do I need to reset all my date fields to US format and convert them back to AU on the forms only?
Thanks for the feedback...
In VBA and VB6, date-literals using the # syntax are always in M/dd/yyyy format, regardless of the user's date format settings. This is a legacy of VBA/VB6's development in the USA before localization was a concern (and the USA is the only country to use the illogical MM/dd/yyyy format).
Note it's M/dd/yyyy and not MM/dd/yyyy - so omit any leading zeroes.
This is documented here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/date-data-type
The same rule also applies to date-literals in Access SQL (not the same thing as VBA/VB6). Fortunately in SQL Server you always use the ISO 8601 yyyy-MM-dd format in quotes... shame about Access though.
You simply need a properly formatted string expression for your date value - and the correct syntax:
Dim NEWDATE As Date
Dim WhereCondition As String
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
WhereCondition = "[NameOfYourDateField] = #" & Format(NEWDATE, "yyyy\/mm\/dd") & "#"
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , WhereCondition
Your first problem is that you're not comparing NewDate to something.
Your second problem is that you're using string concatenation to filter by a date, and that results in the wrong date.
You can view this answer for the different ways to use parameters in Access.
Your final code should probably look something like this:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField = NewDateParam"
Or, if there might be time stored with the date:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField - NewDateParam < 1"

Where do I find the reference for TO_DATE format string

I need to convert a string formatted as MM/DD/YYYY HH:MI plus AM/PM, but can't find a complete reference to the format string to find how to specify the AM/PM part.
I would certainly appreciate information on how to do this, but would appreciate a link to a good source of documentation for this even more.
:EDIT
SELECT top 1
v.CalendarDateTime
,TO_TIMESTAMP(v.CalendarDateTime,'MM/DD/YYYY HH:MIAM') as CalendarDateTimeTS
--,CAST(TO_TIMESTAMP(v.CalendarDateTime,'MM/DD/YYYY HH:MIAM') AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MIBT') AS CalendarDateTimeTS2
12/03/2015 03:00AM 12/3/2015 03:00:00.000000
The commented out line produces a "DateTime field overflow" error.
You probably want TO_TIMESTAMP instead of TO_DATE.
The only bad thing about the Oracle function is the resulting datatype of TIMESTAMP(6) which can't be changed:
TO_TIMESTAMP('12/03/2015 03:00AM', 'MM/DD/YYYY HH:MIAM')
Using Teradata's FORMAT you can specify the timestamp precision, but it's less flexible than Oracle's, the string must match the format exactly:
CAST('12/03/2015 03:00AM' AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYbHH:MIT')
On the Teradata site you'll find the (slow) online docu, e.g. TO_DATE formats or Teradata FORMATs. Of course you should download the full documentation CD for your release.
Please tell us at least which programming language are you using.
Normally it would be something like "MM/DD/YYYY HH:MI a" but we need to know first you language.

DB2 Cast DateTime string with "T" separator

Help me please with the next problem.
I have date time string in the next format(ISO 8601): 1999-12-31T23:59:59
and I need to cast it to TIMESTAMP value. The main problem in 'T' separator character.
I have tried next query:
SELECT TIMESTAMP_FORMAT('1999-12-31T23:59:59','YYYY-MM-DD HH24:MI:SS') FROM ROMAN.EMPLOYEE;
and use different format strings, such as, YYYY-MM-DDTHH24:MI:SS, YYYY-MM-DD"T"HH24:MI:SS.
Could you provide me correct way to cast this type of strings without any character replacement and substrings.
Thanks in advance!
There is no built-in function that can format a timestamp in ISO-8601 format in DB2 for Linux/UNIX/Windows.
As you have probably surmised, you can do this with REPLACE:
select
TIMESTAMP_FORMAT(REPLACE('1999-12-31T23:59:59','T',' '), 'YYYY-MM-DD HH24:MI:SS')
from
ROMAN.EMPLOYEE;
It's trivial to create a user defined function (UDF) to handle this formatting for you as well so you don't have to out this long string in every query.
It may also be possible to do it via XQuery with and xs:dateTime, although this would be even more code than just embedding REPLACE in the call to TIMESTAMP_FORMAT.

Resources