Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting Getdate() To Varchar


Converting Getdate() To Varchar

Author
Message
RockStar
RockStar
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 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!
antonio.collins
antonio.collins
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 921
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 )
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32528 Visits: 38339
Try this:

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

Cool

Cool
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)
John Rowan
John Rowan
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5042 Visits: 4539
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
RockStar
RockStar
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 81
I REALLY appreciate the quick response, exactly what I needed. Wish I had asked earlier. BigGrin
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13069 Visits: 19407
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
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13069 Visits: 19407
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
Exploring Recursive CTEs by Example Dwain Camps
Tommy Bollhofer
Tommy Bollhofer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1552 Visits: 3359
I believe 14 is what your looking for -


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



Tommy

Follow @sqlscribe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search