DateTime to Date format in Oracle

Hi,

How do i convert datetime to date in oracle .

i want to return date in "mm/dd/yyyy" format...
CodingSucksAsked:
Who is Participating?
 
slightwv (䄆 Netminder) Commented:
>>Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....

Not to_date, to_char
0
 
zofcentrCommented:
Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....
0
 
slightwv (䄆 Netminder) Commented:
to_char(datecolumn,'MM/DD/YYYY')
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
sdstuberCommented:
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
 
CodingSucksAuthor Commented:
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
 
slightwv (䄆 Netminder) Commented:
>>it's returning error

That is why I corrected it in http:#a37742370
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
select to_char(to_date(sysdate,'dd/MON/yyyy'), 'mm/dd/yyyy') theDate from dual
0
 
sdstuberCommented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
I did run the query before posting and runs fine on 8i using sql navigator 5.5 (old technology here).
0
 
sdstuberCommented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
CodingSucksAuthor Commented:
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
 
sdstuberCommented:
>>>.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
 
CodingSucksAuthor Commented:
if i can return date in "MM/DD/YYYY" that will solve all my problems..
0
 
sdstuberCommented:
>>> 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
 
CodingSucksAuthor Commented:
I've requested that this question be deleted for the following reason:

solved
0
 
sdstuberCommented:
please choose whichever answer or answers were helpful in solving the problem and grade and split points accordingly
0
 
sdstuberCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.