Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to_char function issue with date passing in the format of 'dd-mon-yyyy'

My query is

select TO_CHAR('03-JAN-2013', 'D') from dual;

but an error occured as

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    
*Action:

But when query changed as select TO_CHAR(sysdate, 'D') from dual;

Result is right answer 5.

I can't understand why it is behaving like this, please help me.

Thanks in advance

like image 694
Taniya Avatar asked Jan 03 '13 09:01

Taniya


People also ask

How do you insert date in DD Mon YY format?

SELECT customer_id, TO_CHAR(dob, 'MONTH DD, YYYY') FROM customers; The next query gets the current date and time from the database using the SYSDATE function, then converts the date and time to a string using TO_CHAR() with the format MONTH DD, YYYY, HH24:MI:SS.

What's the difference between the TO_CHAR and TO_DATE functions when working with date values?

To_char formats a DATE into a string using the given format mask. To_date converts a STRING into a date using the format mask.

How do I display a date in YYYY MM DD format in Oracle?

Use TO_CHAR to display it in any format you like. For example: SELECT TO_CHAR ( TO_DATE (date_value, 'yyyy-mm-dd') , 'mm/dd/yyyy' ) FROM table_x; Things are much easier if you store dates in DATE columns.

What is the TO_CHAR function in SQL Server?

In Oracle, TO_CHAR function converts a datetime value (DATE, TIMESTAMP data types i.e.) to a string using the specified format. In SQL Server, you can use CONVERT or CAST functions to convert a datetime value (DATETIME, DATETIME2 data types i.e.) to a string.


1 Answers

Please cast the string to date before selecting.

SELECT TO_CHAR(CAST('03-JAN-2013' AS DATE), 'D') FROM DUAL;

OR

SELECT TO_CHAR(TO_DATE('03-JAN-2013'), 'D') FROM DUAL;
like image 152
TechDo Avatar answered Sep 28 '22 06:09

TechDo