Convert YYYYMMDD to Excel dd/mm/yy

2019-08-31 01:35发布

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?

2条回答
We Are One
2楼-- · 2019-08-31 02:17

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

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

More Here

查看更多
【Aperson】
3楼-- · 2019-08-31 02:29
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
查看更多
登录 后发表回答