I have a tool which is populating a merge field I am using as part of generated SQL. The format is 113 (04 Aug 2020) and I cannot change it. What is the best way to make DB2 interpret this as a date?
If your date is in the format DD MON YYYY (I don't know what "113" is supposed to mean), then you can use to_date():
to_date(merge_field, 'DD MON YYYY')
Here is a db<>fiddle.
Related
I am having difficulty converting dates in a column from YYYY-MM-DD to Mon DD, YYYY
I think I first need to reorganize the dates and then use a case when statement to specify 01 = Jan and so on? Is that correct?
SELECT to_date(column_name, 'MM/DD/YYYY')
FROM table
gives me some incorrect dates
i.e. previous = 2012-01-29 and
result from query = 0197-06-26
Any suggestions? Thanks
I figured it out!
SELECT to_char(date(column_name), 'Mon dd, yyyy')
FROM table
gives me exactly what I need without the need of a case statement.
Greeting Community.
I saw similar examples in Java but how do we do it it SQL in particular Oracle.
For now I just do this which it works because we are in PDT timezone.
-Thx for the help!
with xx as (
select replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)',' GMT-0700 (PDT)','') as dt from dual
)
select to_date(dt,'DY MON DD YYYY HH24:MI:SS') from xx
The input is a timestamp with time zone. It has redundant information about the time zone; some of that must be stripped away. For example, if you choose to trust PDT and ignore the GMT offset, you could do something like this:
with xx as (
select regexp_replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)','GMT-\d{4} \(|\)')
as str_ts_with_tz
from dual
)
select to_timestamp_tz(str_ts_with_tz,'Dy Mon DD YYYY HH24:MI:SS TZD')
as oracle_ts_with_tz
from xx
;
ORACLE_TS_WITH_TZ
---------------------------------------
2019-08-19 08:21:48 America/Los_Angeles
Note that the resulting data type is timestamp with time zone. You can further cast this as timestamp or as date (simply discarding the time zone information) if needed; this is a simple operation, internal to Oracle. But that will lose information; think twice before you do that.
Note also that the timestamp is presented in a different format in my output, compared to your input. This is because my NLS_TIMESTAMP_TZ_FORMAT is 'yyyy-mm-dd hh24:mi:ss tzr'.
If you are wondering why you must make a choice (which, then, means "why you need to delete part of the input string first") regarding the time zone information: PDT is simply not the same as GMT-0700. It may be that in summer, but not in winter. Oracle won't accept such self-contradicting nonsense as input to its functions. Either it's GMT-0700 or it's PDT, it can't be both. And, you can't just use REPLACE (not easily, anyway), because what must be replaced may have variable characters in it.
I'm trying to convert the following String to a datetime format:
Sat Jan 14 13:55:34 CET 2017
If we consider that my date is in the in_OutPut3 variable, I'm trying to use the following format pattern :
TO_DATE(in_OutPut3,'DY MON DD HH:MI:SS Z YYYY')
I receive the following error :
The system failed to parse the date format : 'DY MON DD HH:MI:SS Z
YYYY'.
Do you have any idea what is this date format ?
You have a timezone embedded in your string so try using the TO_TIMESTAMP_TZ function. TO_DATE is not designed to work with timezones.
The following works on 11g;
select to_timestamp_tz('Sat Jan 14 13:55:34 CET 2017','DY MON DD HH24:MI:SS TZR YYYY') from dual
This one worked for me:
to_timestamp('Sat Jan 14 13:55:34 CET 2017','Dy Mon dd hh:mi:ss ZZ yyyy');
Marco
You can just ignore the timezone while converting to Informatica Date type. Anyway, Informatica won't retain timezone information after converting to Data datatype. Put some symbols like underscore(_) to match 'CET'
TO_DATE(in_OutPut3, 'DY MON DD HH24:MI:SS ___ YYYY')
Also, looks like you have the hours in military format. In that case you have to use HH24.
I need to convert Decimal(17,9) datatype to Timestamp(6).
Example, I am having Decimal(17,9) value as 20150619.154519 and I need to convert it to timestamp like Jun 19, 2015 15:45.
You have a strange format. First convert it to a string, then to a date:
select to_date(to_char(col, '99999999.999999'), 'YYYYMMDD.HHMISS')
Actually, this would probably work without the to_char() part, but it seems safer to do the conversion explicitly.
I want the date in DD-MMM-YYYY format eg 29-JAN-2015.
I have tried with:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YY')) FROM DUAL
I got result as: 29-JAN-15
But I am expecting: 29-JAN-2015 in date format not in char format
Im assuming Oracle DB:
select to_char(SYSDATE, 'dd-Mon-yyyy') from dual
Returns
29-Jan-2015
Thanks for answers.
I got the solution. First we need to alter the session as below:
alter session set nls_date_format='DD-MON-YYYY';
then run the query:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YYYY'))
FROM DUAL
Now I got result as:29-JAN-2015
What you are doing is take the string '29 Jan 2015' and make it a date using the format 'DD MON YY'. This should fail of course for '2015' not matching 'yy', but Oracle is lenient here.
Then you use TRIM on the date. But TRIM is for strings. What happens is that you get shown '29 Jan 15'. I am getting shown '29.01.15' instead of the usual '29.01.2015'. However the behavior: Don't use TRIM on dates, its behavior is nowhere specified as far as I am aware. Use TO_CHAR to format a date in output.
If you only select a date without TO_CHAR you get the date shown in some standard format, which can be '29 Jan 2015' or '29 Jan 15' or '29.01.2015' or '01/29/2015' depending on the app you are using and possibly some setting therin.
For completeness sake:
TO_DATE takes a string ('29 Jan 2015' in your case) and converts it to a date. If the string contains names, make sure that you specify the appropriate language setting:
TO_DATE('29 Jan 2015', 'dd mon yyyy', ''NLS_DATE_LANGUAGE=AMERICAN')
To get a formatted string from a date, use TO_CHAR:
TO_CHAR(sysdate, 'dd MON yyyy', 'NLS_DATE_LANGUAGE=AMERICAN')
Usually you don't do that, however. You select a date as is and have your app (written in PHP, Java or whatever) care about how to display it appropriately to the user's computer's settings.
In SQL Server the query
select CONVERT(nvarchar, GETDATE(), 106) as [Converted Date]
returns:
29 Jan 2015
Manu is correct. Oracle publish a full list of date format specifiers.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA
Use CONVERT(VARCHAR(11),GETDATE(),106)
See detailed explanation here:
http://www.w3schools.com/sql/func_convert.asp
Can you try:
SELECT TRIM(TO_DATE(SYSDATE ,'DD MON YYYY')) FROM DUAL
YY will only show 2 ciphers, instead of YYYY that will show 4.