In SQL Server how do I format getdate()
output into YYYYMMDDHHmmSS
where HH
is 24 hour format?
I\'ve got the YYYYMMDD
converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
Put them together like so inside the function
DECLARE @result as varchar(20)
set @result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
print @result
20131220 13:15:50
As Thinhbk posted you can use select CONVERT(varchar,getdate(),20)
or select CONVERT(varchar,getdate(),120)
to get quite close to what you want.