x

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.

more ▼

asked Aug 18, 2012 at 05:42 PM in Default

avatar image

innap9999
38 7 9 12

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Try

select REPLACE(REPLACE(REPLACE(replace (convert(varchar(30), GETDATE(),121), '-', ''), ' ', ''), ':', ''), '.', '')

more ▼

answered Aug 18, 2012 at 06:38 PM

avatar image

SirSQL
4.9k 4 5

(comments are locked)
10|1200 characters needed characters left

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!

more ▼

answered Dec 31, 2015 at 12:57 PM

avatar image

JohnM
12.4k 3 7 14

My fault, I just realized that this is a very old thread.

Dec 31, 2015 at 01:43 PM JohnM
(comments are locked)
10|1200 characters needed characters left

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

more ▼

answered Dec 31, 2015 at 10:30 AM

avatar image

Gazz
980 3 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x80

asked: Aug 18, 2012 at 05:42 PM

Seen: 947 times

Last Updated: Dec 31, 2015 at 01:43 PM

Copyright 2016 Redgate Software. Privacy Policy