February 6, 2008 at 9:52 am
Trying to update a timestamp field with getdate(). Field is Varchar (16) and the time needs to be in the following format : YYYYMMDDhhmmssms
Example: 2008020609435709
Using the code below to get the second, but in desperate need of how to include the milliseconds in this format.
Select convert(varchar(8),getdate(),112) -- YYYYMMDD
+substring(convert(varchar(8),getdate(),108),1,2) --HH
+substring(convert(varchar(8),getdate(),108),4,2) -- mm
+substring(convert(varchar(8),getdate(),108),7,2) --ss
Thanks in Advance for your advice!
February 6, 2008 at 10:03 am
you can just convert the whole thing and strip out the characters. milliseconds is 3 digits so you'll have to truncate the result from 17 to 16 digits.
select substring( replace(replace( replace(replace( convert(varchar,getdate(),121),' ',''),'-',''),':',''),'.',''), 1, 16 )
February 6, 2008 at 10:04 am
Try this:
select replace(replace(replace(replace(convert(varchar(30), getdate(), 121),'-',''),' ',''),':',''),'.','')
๐
February 6, 2008 at 10:09 am
SELECT LEFT(REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(varchar,GETDATE(), 121),'-',''),':',''),'.',''),' ',''),16)
February 6, 2008 at 10:11 am
I REALLY appreciate the quick response, exactly what I needed. Wish I had asked earlier. ๐
February 6, 2008 at 10:21 am
Here's a couple of ideas.
DECLARE @Now DATETIME
SET @Now = GETDATE()
SELECT @Now
SELECT CAST(datepart(yy, @Now) AS CHAR(4)) +
RIGHT('0'+LTRIM(cast(datepart(mm, @Now) as VARCHAR(2))), 2) +
RIGHT('0'+LTRIM(cast(datepart(dd, @Now) as VARCHAR(2))), 2) +
RIGHT('0'+LTRIM(cast(datepart(hh, @Now) as VARCHAR(2))), 2) +
RIGHT('0'+LTRIM(cast(datepart(mm, @Now) as VARCHAR(2))), 2) +
RIGHT('0'+LTRIM(cast(datepart(ss, @Now) as VARCHAR(2))), 2) +
RIGHT('00'+LTRIM(cast(datepart(ms, @Now) as VARCHAR(3))), 3)
SELECT REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(28), @Now, 121), '-', ''), ' ', ''), ':', ''), '.', '')
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 6, 2008 at 10:22 am
D'oh!:blush:
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 6, 2008 at 10:26 am
I believe 14 is what your looking for -
select replace(convert(varchar, getdate(),112),'/','') + replace(convert(varchar, getdate(),14),':','')
Tommy
Follow @sqlscribeViewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply