Solved

DateTime to Date format in Oracle

Posted on 2012-03-20
22
1,468 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
  • 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 76

Expert Comment

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

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
 
LVL 73

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 76

Expert Comment

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

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

Expert Comment

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

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 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

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 73

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 39

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 73

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 73

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 73

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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now