mrong
asked on
Convert Date field in oracle
Greeting,
I have a date field in Oracle in dd-mmm-yy format. Now I want to convert it to MM/dd/yyyy format. How to do it in a select stmt in oracle?
Thanks
I have a date field in Oracle in dd-mmm-yy format. Now I want to convert it to MM/dd/yyyy format. How to do it in a select stmt in oracle?
Thanks
to_char(yourdate,'MM/dd/yy yy')
Dates are stored as a number in Oracle. How they are 'displayed' is based on the NLS_DATE_FORMAT parameter of a TO_CHAR call:
select to_char(date_column,'MM/DD /YYYY') from table;
select to_char(date_column,'MM/DD
select to_char(sysdate,'MM/dd/yyy y') from dual
would give you
TO_CHAR(SY
----------
02/11/2011
would give you
TO_CHAR(SY
----------
02/11/2011
>>dd-mmm-yy
I should have added that without a TO_CHAR and NLS_DATE_FORMAT set, the 'default' is DD-MON-YY which is what you are seeing.
I should have added that without a TO_CHAR and NLS_DATE_FORMAT set, the 'default' is DD-MON-YY which is what you are seeing.
date's don't have formats, strings have formats
is your field really a date?
if so, just select it with the format you want
if your field is really a string, then convert it to a date, then convert that date to a string
to_char(to_date(your_field ,'dd-mmm-r r'), 'MM/dd/yyyy')
is your field really a date?
if so, just select it with the format you want
if your field is really a string, then convert it to a date, then convert that date to a string
to_char(to_date(your_field
ASKER
This won't work since I need to compare this date to date range below.
to_char(yourdate,'MM/dd/yy yy') between ? and ?
? is in mm/dd/yyyy format.
thanks.
to_char(yourdate,'MM/dd/yy
? is in mm/dd/yyyy format.
thanks.
ASKER
If I do the following comparison, it won't give me the correct output.
to_char(yourdate,'MM/dd/yy yy') between ? and ?
? is in mm/dd/yyyy format.
to_char(yourdate,'MM/dd/yy
? is in mm/dd/yyyy format.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if "yourdate" is actually a string then
you must convert it to a date too
to_date(yourdate,'whatever format is appropriate for this string')
between to_date(?,'mm/dd/yyyy') and to_date(?,'mm/dd/yyyy').
you must convert it to a date too
to_date(yourdate,'whatever
between to_date(?,'mm/dd/yyyy') and to_date(?,'mm/dd/yyyy').
< Now I want to convert it to mm/dd/yyyy format
Please post your Table Structure or DATA types for all the 3 columns involved in this SQL.
If all columns are dates then
where yourdate1 between date2 and date3
Please post your Table Structure or DATA types for all the 3 columns involved in this SQL.
If all columns are dates then
where yourdate1 between date2 and date3
You can do this:
SQL> alter session set NLS_DATE_FORMAT='MM/dd/yyy y';
use your select statement like:
select column_a, column_b from table where date_column between 'first date' and 'second date' (formatted as MM/DD/YYYY for first and second date)
select column_a, column_b from table where date_column between '01/012011' and '01/31/2011''
SQL> alter session set NLS_DATE_FORMAT='MM/dd/yyy
use your select statement like:
select column_a, column_b from table where date_column between 'first date' and 'second date' (formatted as MM/DD/YYYY for first and second date)
select column_a, column_b from table where date_column between '01/012011' and '01/31/2011''