In SQL Server how do I format getdate()
output into YYYYMMDDHHmmSS
where HH
is 24 hour format?
I've got the YYYYMMDD
done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.
In SQL Server how do I format getdate()
output into YYYYMMDDHHmmSS
where HH
is 24 hour format?
I've got the YYYYMMDD
done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.
Try this:
select CONVERT(varchar, GETDATE(), 120) e.g.
2011-09-23 12:18:24 (yyyy-mm-dd hh:mi:ss (24h) ,ODBC canonical).
Hth.
Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:
This allows any .NET format strings making it a useful new addition.
converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
Put them together like so inside the function
20131220 13:15:50
As Thinhbk posted you can use
select CONVERT(varchar,getdate(),20)
orselect CONVERT(varchar,getdate(),120)
to get quite close to what you want.Another option!
Close but not exactly what you are asking for:
e.g.
(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.