Solved

Convert Date field in oracle

Posted on 2011-02-11
11
707 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:mrong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 18

Expert Comment

by:sventhan
ID: 34874622
to_char(yourdate,'MM/dd/yyyy')
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34874632
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;
0
 
LVL 18

Expert Comment

by:sventhan
ID: 34874647
select to_char(sysdate,'MM/dd/yyyy') from dual

would give you

TO_CHAR(SY
----------
02/11/2011
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34874660
>>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.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34874670
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-rr'), 'MM/dd/yyyy')
0
 

Author Comment

by:mrong
ID: 34874689
This won't work since I need to compare this date to date range below.

to_char(yourdate,'MM/dd/yyyy')  between ? and ?

? is in mm/dd/yyyy format.

thanks.
0
 

Author Comment

by:mrong
ID: 34874722
If I do the following comparison, it won't give me the correct output.

to_char(yourdate,'MM/dd/yyyy')  between ? and ?
? is in mm/dd/yyyy format.

0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34874765
again

dates do NOT have formats,  only strings have formats


if   ? and ?  are strings in mm/dd/yyyy format then "between" doesn't make sense because they will be sorted in alphabetical order,  not date order.


compare dates to dates  and strings to strings

yourdate between to_date(?,'mm/dd/yyyy') and to_date(?,'mm/dd/yyyy').

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34874777
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').

0
 
LVL 18

Expert Comment

by:sventhan
ID: 34874884
<  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
0
 
LVL 4

Expert Comment

by:leewv1
ID: 34874963
You can do this:

SQL> alter session set NLS_DATE_FORMAT='MM/dd/yyyy';

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''
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question