我尝试将YYYYMMDD
转换为MM/DD/YY
并使用下面的代码,但无效:
Convert(Varchar(8),Convert(Datetime,CONVERT(VARCHAR(8),s.birth_date)),101) DOB
有人可以帮助我。
答案 0 :(得分:1)
这应该让你开始:
;WITH CTE AS
(
SELECT birth_date As IntDate,
LEFT(CAST(birth_date as char(8)), 4) +'-'+ SUBSTRING(CAST(birth_date as char(8)), 5, 2) +'-'+ RIGHT(CAST(birth_date as char(8)), 2) As ISOFormat,
SUBSTRING(CAST(birth_date as char(8)), 5, 2) +'/'+ RIGHT(CAST(birth_date as char(8)), 2) +'/'+ LEFT(CAST(birth_date as char(8)), 4) As USFormat
FROM YourTable
WHERE birth_date > 9999999 -- must have 8 digits
AND birth_date < 99991232 -- last supported date is 9999-12-31
)
SELECT IntDate, ISOFormat, USFormat
FROM CTE
WHERE ISDATE(ISOFormat) = 1