question

innap9999 avatar image
innap9999 asked

Need to get date and time in format of 20120817133600

Hello, I need to get date and time in format of 20120817133600. Tried use Convert Replace, but without success, Please help. Thank you in advance.
datetime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SirSQL avatar image
SirSQL answered
Try select REPLACE(REPLACE(REPLACE(replace (convert(varchar(30), GETDATE(),121), '-', ''), ' ', ''), ':', ''), '.', '')
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gazz avatar image
Gazz answered
You can get the date part easily using a convert: SELECT CONVERT(date, '20120817') but to get the date and time I would use STUFF to split it up: select convert(datetime,stuff(stuff(stuff('20120817133600', 9, 0, ' '), 12, 0, ':'), 15, 0, ':')) [ https://msdn.microsoft.com/en-GB/library/ms188043.aspx][1] [1]: https://msdn.microsoft.com/en-GB/library/ms188043.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image
JohnM answered
If you are using SQL Server 2012 and above, you can use the FORMAT function to do this very easily. DECLARE @d DATETIME = GETDATE(); SELECT FORMAT( @d, 'yyyyddMMhhmmssms' ) AS 'DateTime Result'; For reference: https://msdn.microsoft.com/en-us/library/hh213505.aspx Hope that helps!
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
My fault, I just realized that this is a very old thread.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.