问题
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