Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DateTime to Date format in Oracle

Posted on 2012-03-20
22
Medium Priority
?
1,797 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 2000 total points
ID: 37742370
>>Use function SELECT TO_DATE(DATETIME_COLUMN,'MM/DD/YYYY') FROM ....

Not to_date, to_char
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 41

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 41

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 41

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

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!

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

610 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