Extracting date time formats can be a challenge.
Below are some common formats which I see used regularly:
TSQL | Format |
SELECT CONVERT(char(8),GETDATE(),112) |
yyyymmdd |
SELECT CONVERT(char(8),GETDATE(),108) |
hh:mm:ss |
SELECT REPLACE(CONVERT(char(8),GETDATE(),108),':','') |
hhmmss |
SELECT CONVERT(char(23), GETDATE(), 121) |
yyyy-mm-dd hh:mm:ss.fff |
SELECT CONVERT(char(19), GETDATE(), 121) |
yyyy-mm-dd hh:mm:ss |
SELECT CONVERT(char(10), GETDATE(), 121) |
yyyy-mm-dd |
SELECT REPLACE(REPLACE(REPLACE(CONVERT(varchar(19), GETDATE(), 121),'-',''),':',''),' ','_') |
yyyymmdd_hhmmss |
SELECT REPLACE(CONVERT(varchar(19), GETDATE(), 121),'-','') |
yyyymmdd hh:mm:ss |