Solved

DateTime to Date format in Oracle

Posted on 2012-03-20
22
1,642 Views
Last Modified: 2012-05-31
Hi,

How do i convert datetime to date in oracle .

i want to return date in "mm/dd/yyyy" format...
0
Comment
Question by:CodingSucks
[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
  • 7
  • 4
  • 4
  • +2
22 Comments
 
LVL 3

Expert Comment

by:zofcentr
ID: 37742366
Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37742367
to_char(datecolumn,'MM/DD/YYYY')
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 37742370
>>Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....

Not to_date, to_char
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 74

Expert Comment

by:sdstuber
ID: 37742399
it will depend on what you are calling "datetime"

if you mean a timestamp data type, then you can convert it to a date easily enough

cast(yourtimestamp as date)

if you mean a string type, then use to_char as shown above.

also this doesn't make sense:
 "i want to return date in "mm/dd/yyyy" format..."

a date does not have a format.  a date is just a date.  A string has a format.

If your goal is to create a date data type without the time piece, you can't.  Dates always have times.  You might not show them, but they are still there.

If that is your goal and then simply use TRUNC on an existing date or timestamp type value

trunc(yourdatevalue)  will produce a date value with time of 00:00:00
0
 

Author Comment

by:CodingSucks
ID: 37742423
Zo and Slight ,

thanks for your response..

SELECT TO_DATE(ENDDATETIME,'MM/DD/YYYY') FROM COLOR  . it's returning error

ORA-01843: not a valid month
01843. 00000 -  "not a valid month"

PN
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37742429
>>it's returning error

That is why I corrected it in http:#a37742370
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37742490
select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37742506
this doesn't make any sense


to_date(sysdate,'dd/MON/yyyy')

sysdate is already a date.

if your goal was to remove the time piece, then, as already mentioned above,  use TRUNC

trunc(sysdate)

BUT...
that's totally unnecessary since the to_char will format it without the extra step

select to_char(sysdate, 'mm/dd/yyyy') theDate from dual
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37742508
ged325,

Did you try running that before you posted it?  

That syntax is incorrect as well:  "to_date(sysdate".  sysdate is already a date.  You do not call to_date on a date.

Fix that and you are left with to_char which has already been posted.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37742535
I did run the query before posting and runs fine on 8i using sql navigator 5.5 (old technology here).
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37742544
ged325 - the reason slightwv asked if you had tested your results is because it might have worked, it might have failed and it might failed in different ways

for example...



SQL> select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual;
select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual
                       *
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected


SQL> alter session set nls_date_format='mm/dd/yyyy';

Session altered.

SQL> select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual;
select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual
                       *
ERROR at line 1:
ORA-01843: not a valid month


SQL> alter session set nls_date_format='dd/mm/yyyy';

Session altered.

SQL> select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual;
select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual
                       *
ERROR at line 1:
ORA-01843: not a valid month


SQL> alter session set nls_date_format='dd/mon/yyyy';

Session altered.

SQL> select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual;

THEDATE
----------
03/20/2012


in any case, as explained above, it's not only inefficient, but as you can see, it's not reliable
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37742574
Understood . . . still learning oracle but good to know.  Thanks for letting me know.  I know sysdate is already a date but formatting it like that doesn't remove the time section?
0
 

Author Comment

by:CodingSucks
ID: 37742638
I have parameters defined in SSRS in dateformat ("mm/DD/YYYY")...my oracle query returns date column as string ("MM/DD/YYYY")...i am having issue comparing string to date...

Any better solution for this..date parameters in SSRS are in MM/DD/YYYY by default...

ged and sd  Thanks for detailed explanation

PN
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37742694
>>>.i am having issue comparing string to date...

don't try.


compare strings to strings  and dates to dates

also,  just as mnemonic to help you keep track  
don't call anything with a format a "date"

"date parameters in SSRS are in MM/DD/YYYY by default..."

you have string parameter in MM/DD/YYYY format  

It's not only technically correct, it's also helpful mentally to think about what you need to do.  

If you find yourself saying I'll take this "string" and combine it with this "date"  you know immediately that you're doing something wrong.
0
 

Author Comment

by:CodingSucks
ID: 37742744
if i can return date in "MM/DD/YYYY" that will solve all my problems..
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37742756
>>> return date in "MM/DD/YYYY"

you can't, dates don't have formats.

you can return a string in "MM/DD/YYYY"  


if you want to change a date to  a string then use TO_CHAR as slightwv showed above.  to_char(yourdate,'mm/dd/yyyy')
0
 

Author Comment

by:CodingSucks
ID: 37979984
I've requested that this question be deleted for the following reason:

solved
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37979985
please choose whichever answer or answers were helpful in solving the problem and grade and split points accordingly
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37990244
there is some good info in the followup conversation about some of the abiguity in the question itself but for PAQ quality I recommend simply accepting slightwv's answer http:#a37742370 as the answer to the direct question of returning a formatted date value.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

705 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