Convert number to date sql oracle

房东的猫 提交于 2019-12-07 04:45:13

问题


I'm trying to convert a number (yyyymmdd) to date (mm/dd/yyyy)

For example

20150302 ====> 03/02/2015

回答1:


You can try this:

select to_date(20150302,'yyyymmdd') from dual;

or

select to_char(to_date(20150302,'yyyymmdd'),'mm/dd/yyyy') from dual;



回答2:


You can use TO_DATE function to convert NUMBER to DATE. Try in following:

SELECT TO_DATE(20150302, 'YYYYMMDD') FROM DUAL



回答3:


TO_DATE accepts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype and converts into a value of DATE datatype.

So, convert the number into string, and apply to_date. You could use single-quotation marks around the number to convert it into string.

SELECT TO_DATE('20150302', 'YYYYMMDD')  FROM dual;

Remember, a DATE has no format, what you see is for display purpose. If you want to display the date in a desired format, then use TO_CHAR along with your desired format model.

SELECT TO_CHAR(TO_DATE('20150302', 'YYYYMMDD'), 'mm/dd/yyyy') FROM dual;

Read more about TO_DATE.



来源:https://stackoverflow.com/questions/31405833/convert-number-to-date-sql-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!