Home » SQL & PL/SQL » SQL & PL/SQL » To_CHAR Problem
|
|
|
|
|
|
|
Re: To_CHAR Problem [message #206359 is a reply to message #206306] |
Wed, 29 November 2006 13:40   |
Ronald Beck
Messages: 121 Registered: February 2003
|
Senior Member |
|
|
A date field in Oracle is a decimal number with the portion left of the decimal representing the date and the portion right of the decimal representing the time.
When humans type in a date, we're typing in a string. Your statement...
select TO_CHAR(sysdate,'Month') as Now_Month from dual;
works because the FUNCTION sysdate returns a numeric value. You use the TO_CHAR function to convert the number (the decimal date) to a string you can read. Furthermore, because of the format you selected, only the 'Month' portion gets displayed.
The value you entered, 29-NOV-06, is a STRING, not a decimal number representing a date. The command...
select to_date('29-NOV-06','DD-MON-RR') from dual;
would provide you with the decimal date. The 'DD-MON-RR' is the format that your string is in. If you used '29-11-06', you would have to include the format string 'DD-MM-RR' because that's the order of your date string. '11/29/2006' would use a format string of 'MM/DD/RRRR' or 'MM/DD/YYYY'. To get just the month from that, you would have to do...
select to_char(to_date('29-NOV-06',DD-MON-RR'),'Month') from dual;
which says, change the string '29-NOV-06' to a decimal date and then change that decimal date into a string containing the 'Month'.
If you always remember that the date you type in is a string and you have to change it to a decimal date (to_date) before you can manipulate it or enter it into a table, you'll have better results when working with dates.
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Jun 05 04:44:40 CDT 2025
|