Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Converting Getdate() To Varchar Expand / Collapse
Author
Message
Posted Wednesday, February 06, 2008 9:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 17, 2008 2:20 PM
Points: 42, Visits: 81
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!
Post #452328
Posted Wednesday, February 06, 2008 10:03 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, April 13, 2014 6:32 PM
Points: 438, Visits: 899
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 )
Post #452333
Posted Wednesday, February 06, 2008 10:03 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:33 PM
Points: 22,475, Visits: 30,157
Try this:

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




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #452334
Posted Wednesday, February 06, 2008 10:09 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 12:46 PM
Points: 3,843, Visits: 3,833
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 - by Jeff Moden
Post #452341
Posted Wednesday, February 06, 2008 10:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 17, 2008 2:20 PM
Points: 42, Visits: 81
I REALLY appreciate the quick response, exactly what I needed. Wish I had asked earlier. :D

Post #452344
Posted Wednesday, February 06, 2008 10:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
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
Exploring Recursive CTEs by Example Dwain Camps
Post #452347
Posted Wednesday, February 06, 2008 10:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
D'oh!

“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
Exploring Recursive CTEs by Example Dwain Camps
Post #452348
Posted Wednesday, February 06, 2008 10:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 4:45 PM
Points: 1,157, Visits: 3,247
I believe 14 is what your looking for -

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



Tommy

Post #452353
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse