Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert varchar2 to Date ('MM/DD/YYYY') in PL/SQL

Tags:

date

format

plsql

I need to convert string from varchar to Date in 'MM/DD/YYYY' format. My input string is '4/9/2013' and my expected output is '04/09/2013'. i.e. 2 digit month, 2 digit date and 4 digit year seperated by'/'

I have below data: DOJ varchar2(10 Byte)column has '4/9/2013' value. I am using 'select TO_DATE(DOJ,'MM/DD/YYYY') from EmpTable. But I am getting result as 09-APR-13. I want it in 04/09/2013 format.

Please advise. Thanks in advance.

like image 355
Devashri B. Avatar asked May 02 '13 19:05

Devashri B.


3 Answers

Easiest way is probably to convert from a VARCHAR to a DATE; then format it back to a VARCHAR again in the format you want;

SELECT TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') FROM EmpTable;

An SQLfiddle to test with.

like image 128
Joachim Isaksson Avatar answered Nov 17 '22 18:11

Joachim Isaksson


First you convert VARCHAR to DATE and then back to CHAR. I do this almost every day and never found any better way.

select TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') from EmpTable
like image 20
hol Avatar answered Nov 17 '22 19:11

hol


Example query: SELECT TO_CHAR(TO_DATE('2017-08-23','YYYY-MM-DD'), 'MM/DD/YYYY') FROM dual;

like image 2
samba siva Avatar answered Nov 17 '22 19:11

samba siva