how to format getdate into YYYYMMDDHHmmSS

Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:

SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'

This allows any .NET format strings making it a useful new addition.


select replace(
       replace(
       replace(convert(varchar(19), getdate(), 126),
       '-',''),
       'T',''),
       ':','')

Close but not exactly what you are asking for:

select CONVERT(varchar, GETDATE(), 126)

e.g.

2011-09-23T12:18:24.837

(yyyy-mm-ddThh:mi:ss.mmm (no spaces), ISO8601 without timezone)

Ref: CAST and CONVERT

There is no way to specify a custom format with CONVERT(). The other option is to perform string manipulation to create in the format you desire.