Tuesday, September 13, 2011

Convert Datetime into YYYYMMDDHHMMSS format


Converting Datatime value in YYYYMMDDHHMMSS format is not that complex as it sounds :)

SELECT CONVERT(VARCHAR(10),GETDATE(),112) --Date alone
SELECT CONVERT(VARCHAR(10),GETDATE(),108) - Time alone


The only problem is in the Time portion we would get ":" separator within HH:MM:SS. So we need to just replace colon with space and concatenate both this result to get an output in the expected format.

Solution:

SELECT 
   CONVERT(VARCHAR(10),GETDATE(),112) + 
   REPLACE(CONVERT(VARCHAR(10),GETDATE(),108),':','') 
          AS [YYYYMMDDHHMMSS]

2 comments:

Anonymous said...

Thank U very much Vadivel!..I was just looking for this

Anonymous said...

Hey,

How can you do the opposite?

Thx