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


Date conversion issue


Date conversion issue

Author
Message
saidapurs
saidapurs
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 414
Hi All,

Using below conversion i am getting date only like 20120926 but my requirement is 201209261015 like hour & time

convert(nvarchar(50),(GetDate()),112))

Please help me to get my exact answer...


Thanks & Regards
Satish
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8979 Visits: 19028
saidapurs (9/26/2012)
Hi All,

Using below conversion i am getting date only like 20120926 but my requirement is 201209261015 like hour & time

convert(nvarchar(50),(GetDate()),112))

Please help me to get my exact answer...


Thanks & Regards
Satish


Style 112 excludes time, returning only date.
See CONVERT in Books Online for more information.
There is no value for Style which will return datetime in the format you require,
you will have to use something like 120, then remove the unwanted characters.

You don't need a double-byte character set to hold numbers.
Match your variable to your data - '201209261015' is CHAR(12)

“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
tim_harkin
tim_harkin
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 898
Not the most efficient solution....

DECLARE @currentTime DATETIME

SET @currentTime = GETDATE()

SELECT CAST(DATEPART(year,@currentTime) AS CHAR(4))
+ CASE WHEN (DATEPART(month,@currentTime)) > 10 then (CAST(DATEPART(month,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(month,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(day,@currentTime)) > 10 THEN (CAST(DATEPART(day,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(day,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(hour,@currentTime)) > 10 THEN (CAST(DATEPART(hour,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(hour,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(minute,@currentTime)) > 10 THEN (CAST(DATEPART(minute,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(minute,@currentTime) AS CHAR(1)) END


ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8979 Visits: 19028
tim_harkin (9/26/2012)
Not the most efficient solution....

DECLARE @currentTime DATETIME

SET @currentTime = GETDATE()

SELECT CAST(DATEPART(year,@currentTime) AS CHAR(4))
+ CASE WHEN (DATEPART(month,@currentTime)) > 10 then (CAST(DATEPART(month,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(month,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(day,@currentTime)) > 10 THEN (CAST(DATEPART(day,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(day,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(hour,@currentTime)) > 10 THEN (CAST(DATEPART(hour,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(hour,@currentTime) AS CHAR(1)) END
+ CASE WHEN (DATEPART(minute,@currentTime)) > 10 THEN (CAST(DATEPART(minute,@currentTime) AS CHAR(2)))
ELSE '0' + CAST(DATEPART(minute,@currentTime) AS CHAR(1)) END



Tim, here's a trick for you;
SELECT CAST(DATEPART(year,@currentTime) AS CHAR(4))
+ RIGHT('0'+CAST(DATEPART(month,@currentTime) AS VARCHAR(2)),2)
+ RIGHT('0'+CAST(DATEPART(day,@currentTime) AS VARCHAR(2)),2)
+ RIGHT('0'+CAST(DATEPART(hour,@currentTime) AS VARCHAR(2)),2)
+ RIGHT('0'+CAST(DATEPART(minute,@currentTime) AS VARCHAR(2)),2)



Here's a quick way to meet OP's requirement;

SELECT CAST(REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),@currentTime,120),'-',''),' ',''),':','') AS CHAR(12))



“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
tim_harkin
tim_harkin
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 898
Thanks!
swaroop.sahoo
swaroop.sahoo
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 267
Hi,

I think this will help

SELECT CONVERT(VARCHAR(10), GETDATE(), 112) + Replace(CONVERT(VARCHAR(10), GETDATE(), 108),':','')

Thanks,
Swaroop
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