본문 바로가기

DB/mssql

[mssql] convert yyyyMMddhhmmss string to datetime



DECLARE @stringDatetime     CHAR(14) = '20160823160000'         -- yyyyMMddhhmmss
DECLARE @convertedDatetime  DATETIME = NULL

SELECT @convertedDatetime = CAST((SUBSTRING(@stringDatetime,1,4) + '-' + 
                                SUBSTRING(@stringDatetime,5,2) + '-' + 
                                SUBSTRING(@stringDatetime,7,2) + ' ' + 
                                SUBSTRING(@stringDatetime,9,2) + ':' + 
                                SUBSTRING(@stringDatetime,11,2) + ':' + 
                                SUBSTRING(@stringDatetime,13,2)) AS DATETIME)

SELECT @convertedDatetime AS convertedDatetime
참고 :  [MSSQL] SUBSTRING 문자열 자르기
demo : http://rextester.com/TWLEM83934