Converting Getdate() To Varchar

  • 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!

  • 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 )

  • Try this:

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

    ๐Ÿ˜Ž

  • SELECT LEFT(REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(varchar,GETDATE(), 121),'-',''),':',''),'.',''),' ',''),16)

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • I REALLY appreciate the quick response, exactly what I needed. Wish I had asked earlier. ๐Ÿ˜€

  • 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), '-', ''), ' ', ''), ':', ''), '.', '')

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    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

  • D'oh!:blush:

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    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

  • I believe 14 is what your looking for -

    select replace(convert(varchar, getdate(),112),'/','') + replace(convert(varchar, getdate(),14),':','')

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply