Oracle Date formatting "2009-02-13T11:46:40+00:00" - oracle

I've had some brilliant help before and I'm hoping you can get me out of a hole again.
I've got a date coming in from a web service in this format:
2009-02-13T11:46:40+00:00
which to me looks like standard UTC format.
I need to insert it into an Oracle database, so I'm using to_date() on the insert. Problem is, I cant get a matching formatting string for it and keep getting "ORA-01861: literal does not match format string" errors.
I know its a fairly trivial problem but for some reason I cannot get it to accept the right format string. Any help appreciated.
Thanks :)
Gareth

You can directly convert it to a TIMESTAMP_WITH_TIME_ZONE datatype.
select
to_timestamp_tz('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from
dual
TO_TIMESTAMP_TZ('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM
---------------------------------------------------------------------------
13-FEB-09 11.46.40.000000000 AM +00:00
(I'm assuming the input string is using a 24-hour clock since there is no AM/PM indicator.)
If you want to convert that to a simple DATE, you can, but it will lose the time zone information.

SELECT CAST(TO_TIMESTAMP_TZ(REPLACE('2009-02-13T11:46:40+00:00', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') AS DATE)
FROM dual

To import date in specified format you can set nls_date_format.
Example:
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'
This way your SQL statements can be shorter (no casts). For various mask look at Datetime Format Models

Related

timestamp conversion issue in oracle

I am trying to convert data from oracle date field which is in local eastern time to UTC format
and I'm using below function to achieve it
CAST("date field" AS TIMESTAMP) AT TIME ZONE 'UTC'
and my output looks like below
06-NOV-19 09.55.21.000000 AM
However i need to format the output to below format
11/6/2019 9:55:21.000000000 AM
Is there an oracle function which i directly use to do above format conversion?
Formatting and the actual value are separate things. The value, you say is correct, however you want it to look in a different style than the default.
Look into TO_CHAR if you want a specifically formatted string output. eg:
SELECT TO_char(cast(sysdate as timestamp) at time zone 'UTC','mm/dd/yyyy hh:mi:ssxFF am') from dual;
11/18/2019 07:39:42.000000 pm
I tried on my database and the same consult turn the expected result. Try to confer this web site that will show the config of date in oracle:
http://blog.marvinsiq.com/2018/08/23/formatar-data-e-hora-no-oracle-sql-developer/

How to load files using SQLLDR with date format as yyyymmddhhmmss?

I need to load a table with a .csv file which contains date "20140825145416".
I have tried using (DT date "yyyymmdd hh24:mm:ss") in my control file.
It throws an error as ORA-01821: date format not recognized
I require the data in table as "MM/DD/YYYY HH:MM:SS".
Sample data : 20140825145416
thanks in advance.
Well, I would be remiss if I did not point out that the correct answer is to never store dates as VARCHAR2 data, but make it a proper DATE column and load it like this:
DT DATE "YYYYMMDDHH24MISS"
Formatting is done when selecting. It will make your life so much easier if you ever need to use that date in a calculation.
That out of the way, If you have no control over the database and have to store it as a VARCHAR2, first convert to a date, then use to_char to format it before inserting:
DT CHAR "to_char(to_date(:DT, 'YYYYMMDDHH24MISS'), 'MM/DD/YYYY HH24:MI:SS')"
Note 'MI' is used for minutes. You had a typo where you used 'MM' (months) again for minutes.
I know it's already been said in the previous answer, but it's so important, it's worth repeating. Do not store dates as varchars !!
If your DT column is timestamp then this might work
DT CHAR(25) date_format TIMESTAMP mask "yyyymmddhhmiss"
I used something like this in external tables. Maybe this might help
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
and
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8128892010789

Oracle Date Format Conversion Issue

Am at the end of my tether so hoping someone can help me! I'm really new to Oracle, but do have a SQL background which is why I'm finding this so frustrating!
We have a system that runs Oracle at the back end. I've got very limited access to the system and can only write select queries.
I've written a query that gets the data I want but the date format is coming out as mm dd yyyy what I need is dd/mm/yyyy
I ran SELECT sysdate FROM dual and that come back as:
SYSDATE
03 11 2015
So my select statement reads (action_date is the column in question)
Select username, action_date from adminview
I've tried everything I can think of to change the date format including:
to_date(action_date,'dd/mm/yyyy')
to_date(action_date,'dd/mm/yyyy','nls_language=English')
to_date(to_date(action_date,'mm dd yyyy'),'dd/mm/yyyy')
I've also tried to_char along the same lines.
If you want to format a DATE value, use TO_CHAR():
SELECT username, TO_CHAR(action_date, 'DD/MM/YYYY') AS action_date
FROM adminview;
If it's not a DATE value, then you'll want to convert it to a DATE (based on what it currently looks like), then use TO_CHAR() to format.

Oracle date format issue

I'm trying to export/import data in .csv format using SQLDeveloper. The source and destination databases are Oracle 11g. I'm having a hard time with the date formats. In the exported .csv, I see dates like:
31-AUG-09 11.54.00.000000000 AM
I'm trying to figure out the appropriate format string, but I don't know what the last element is before the meridian indicator (AM/PM). Here's the format string I have.
'DD-MON-YY HH.MI.SS.??????????? AM'
What should take the place of the question marks?
If these values are always 00000000000, then ??????????? could be just fine, in case you use DATE.
If you want to convert those 0s, you need to use a TIMESTAMP and FF9:
SELECT TO_TIMESTAMP( '31-AUG-09 11.54.00.000000000 AM',
'DD-MON-YY HH.MI.SS.FF9 AM' )
FROM dual
You have another problem though: Use MI instead of MM, since MM is month and can not be used twice.
You can use FF9 to represent the fractional seconds part.

Oracle last_ddl_time format

I have to query all_objects table where last_ddl_time='01 jan 2010' but it refuses the date format...
Any body give me the exact format to query?
As AKF said, you should be using Trunc unless you know the exact time the DDL was modified. Your query you added in the comments is looking for any objects where the DDL changed at 1/1/2010 00:00:00. Try:
SELECT *
FROM all_objects
WHERE trunc(last_ddl_time) = to_date('01-01-2010','dd-mm-yyyy');
I suggest you to use de date literal:
where trunc(last_ddl_time) = date '2010-01-01'
You can use the to_date function to format your date. If you enter a literal string, Oracle will attempt to convert that string using to_date with a default format 'DD-MON-YY', so your date would look like "01-JAN-10". As Oracle will be using this same function, you might want to put it in yourself and enjoy the finer granularity that custom formatting can provide.
It would be good to note that the dates stored in that column most likely have more precise dates, including hours and minutes, etc. Though you will be taking a bit of a performance hit, you might be better served using trunc(last_ddl_time) if you are testing with =.
There is some good info on Dates in Oracle at this link.
SELECT *
FROM all_objects t
WHERE trunc(t.last_ddl_time, 'DD') = to_date('2010-JAN-01', 'YYYY-MON-DD');

Resources