Convert YYYYMMDD to Excel dd/mm/yy

十年热恋 提交于 2019-12-25 07:34:46

问题


In this post Gert Grenander makes a suggestion to format the date field to 'yyyy-mm-dd hh:mm:ss'.

How would I convert from 'YYYYMMDD' to 'dd/mm/yy' in my SQL call using the same method?


回答1:


select date2, 
       digits(date2), 
       (substr(digits(date2),7,2) concat '/' concat 
        substr(digits(date2),5,2) concat '/' concat 
        substr(digits(date2),3,2) 
       ) as mmddyy
  from datesample

gives:

  Signed                       CHAR       
 data type  DIGITS ( DATE2 )  MMDDYY  
----------  ----------------  --------
20130711    20130711          11/07/13

You'll need to convert the decimal value (DATE2) to string via DIGITS, then use SUBSTR to extract the pieces you need, then use CONCAT (or ||) to reassemble them including the delimiter you want. If your 'date' column is character, you can leave out the conversion to character.

select date4, 
       (substr(date4,7,2) concat '/' concat 
        substr(date4,5,2) concat '/' concat 
        substr(date4,3,2) 
       ) as mmddyy
  from datesample

gives:

  CHAR     CHAR        
data type  MMDDYY 
---------  --------
20130711   11/07/13



回答2:


You can use CONVERT function in SQL for Converting to desired format

SELECT CONVERT(VARCHAR(15), @your_date, 103)

More Here



来源:https://stackoverflow.com/questions/17585099/convert-yyyymmdd-to-excel-dd-mm-yy

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