Date format to change - oracle

I need help to change the date format from (05 May 2016 12:00 AM) to (YYYY-MM-DD HH:MM:SS) format.
Can you please help me on this?
Many thanks for your help.
I have tried below format
SELECT TO_CHAR(TO_DATE('2 Apr 2015 12:00 AM', 'DD-MON-YYYY '), 'YYYY-MM-DD')
FROM dual;
Thanks for the Help MTO. But i am using same format in sqlldr then getting below error.
ora-01821 date format not recognized
LAST_UPDATE DATE 'TO_CHAR(TO_DATE(:LAST_UPDATE, 'DD MON YYYY HH12:MI AM'),'YYYY-MM-DD HH24:MI:SS')',

Use the full format mask:
SELECT TO_CHAR(
TO_DATE( '2 Apr 2015 12:00 AM', 'DD MON YYYY HH12:MI AM'),
'YYYY-MM-DD HH24:MI:SS'
)
FROM dual;

If you're doing this in SQL*Loader and populating a DATE column then you want your initial string to be converted to a date, not back to another string in another format.
If you use the SQL operator functionality then you only need the inner to_date() part, not the outer to_char(), and notice that the operator is enclosed in double quotes rather than single quotes:
LAST_UPDATE "TO_DATE(:LAST_UPDATE, 'DD MON YYYY HH12:MI AM')",
But there is simpler handling for datetimes and intervals, which you are sort of trying to use with the DATE keyword; but then you're supplying the SQL operators instead of just a format mask. You can just do:
LAST_UPDATE DATE 'DD MON YYYY HH12:MI AM'.
This assumes SQL*Loader is run in an english-language NLS_LANG environment, since it relies on the NLS_DATE_LANGUAGE setting to handle the MON element. If that is not the case then the SQL operator approach can be used, with the optional third argument to to_date() to specify that it is expecting the string to be in English.
Your question seems to be partly confused by thinking that Oracle stores dates with a specific format. It does not; it uses an internal representation which you generally don't need to know about, and it's up to your client to display that internal value in a readable format, which is does using explicit or implicit conversions. You seem to be assuming dates are 'stored' as YYYY-MM-DD HH24:MI:SS, but that is just how your client is displaying the data to you.

Related

How to get future and past dates in mm/dd/yyyy and dd/mm/yyyy format in Oracle SQL 18C?

I want to get display of future and past dates in mm/dd/yyyy and dd/mm/yyyy format in Oracle SQL 18C using SQL functions, so I want the code for it. I tried code select sysdate from dual and I get the output 21-JAN-23, but I want output of future and past dates like 23/11/2033 and 16/12/2009 in mm/dd/yyyy and dd/mm/yyyy format.
Format date using TO_CHAR() function
SELECT
TO_CHAR( SYSDATE, 'FMMonth DD, YYYY' )
FROM
dual;
The output would be:
August 1, 2017
Creating a Future or Past Date
In Oracle, a DATE is a binary data type that ALWAYS consists of 7 bytes representing century, year-of-century, month, day, hour, minute and second and is NEVER stored in any particular human-readable format.
Therefore, if you want to get a DATE data type in a particular format then it is impossible as dates never have any format when they are stored.
If you want to get a date you can use:
A date literal:
SELECT DATE '2023-12-31' FROM DUAL;
or, the TO_DATE function:
SELECT TO_DATE('31/12/2023', 'MM/DD/YYYY') FROM DUAL;
Displaying Dates in a Client Application
However, if the problem is how to display a date in a particular format then you need to convert the binary DATE value to a string.
Most client applications (SQL*Plus, SQL Developer, TOAD, C#, Java, etc.) will implicitly convert a binary date to something that is human-readable when they display it and will have settings in the application that determine the default format that it applies to dates.
For SQL*Plus and SQL Developer, you can modify the NLS_DATE_FORMAT session parameter to change how that client application displays dates (note: this does not change how Oracle stores the dates internally, only how it is displayed by the client).
For example:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
or:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
And then the client application will display dates in that format when you use a SELECT statement.
For other client applications you will need to check the documentation for that application.
Explicitly Formatting Dates as Strings
If you want to display a DATE in a particular format independent of any settings in the client application then you will need to convert the date to a string.
Using TO_CHAR:
SELECT TO_CHAR(DATE '2023-12-31', 'MM/DD/YYYY') AS formatted_date FROM DUAL;
Or, if you are generating the date and formatting it (rather than taking an existing date and formatting it) then you could just use a string literal:
SELECT '31/12/2023' AS formatted_date FROM DUAL;

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 Insert from TH Date Format

I have the Data in the date formats of
2nd November 2010
15th Mar 2013 -- and so on.
I need to pick up these data and insert into the field of type DATE.
How can I achieve that?
select to_char(sysdate,'ddth Month YYYY','NLS_DATE_language=American') from dual
output:
19th November 2014
select to_date('15th Mar 2013','dd"th" Mon YYYY','NLS_DATE_language=American') from dual
used to trans varchar to date format.
Hope helps you.
You just need to cast the string to a date with the appropriate format mask:
insert into your_table values (
to_date('2nd November 2010', 'ddth Month YYYY')
)
The documentation has a complete list of format models. Find out more.
However, you have a problem because your input has different formats. We cannot use the same mask to match NOVEMBER and MAR; the second date requires a mask of 'ddth Mar YYYY'.
So you will need to write a function which catches ORA-01861: literal does not match format string and applies a different mask; depending on the quality of your input data you may need to have several of these. This situation is common with applications which don't use strong data-typing, and so demand data cleansing when they interact with more rigourous systems.

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 Date formatting "2009-02-13T11:46:40+00:00"

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

Resources