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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zofcentrCommented:
Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....
0
slightwv (䄆 Netminder) Commented:
to_char(datecolumn,'MM/DD/YYYY')
0
slightwv (䄆 Netminder) Commented:
>>Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....

Not to_date, to_char
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.