SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trying to convert varchar to datetime


Trying to convert varchar to datetime

Author
Message
savycara
savycara
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 13
DECLARE @d varchar(6) = '20114'
SELECT try_convert(date, substring(@d, 1, 4) +
CASE WHEN len(@d) = 5 THEN '0' ELSE '' END +
substring(@d, 5, 2) + '01')

This worked but I am not getting in 101 style that is 04/01/2011
Erland Sommarskog
Erland Sommarskog
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2127 Visits: 872
You wanted to convert to datetime, didn't you? Datetime is a binary value and includes no format. If you want a text string, just add a conversion to varchar. However, formatting of date values is best done in the client, so that the user's regional settings can be respected.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87098 Visits: 41113
savycara (7/24/2013)
DECLARE @d varchar(6) = '20114'
SELECT try_convert(date, substring(@d, 1, 4) +
CASE WHEN len(@d) = 5 THEN '0' ELSE '' END +
substring(@d, 5, 2) + '01')

This worked but I am not getting in 101 style that is 04/01/2011


If you don't mind a non-Ansi solution (and I don't), this is pretty easy. That, not withstanding, it would be good to heed the warnings about formatting dates on the backend.


WITH
cteTestData(D) AS
( --=== This is test data and is not a part of the solution
SELECT '20111' UNION ALL
SELECT '20112' UNION ALL
SELECT '20113' UNION ALL
SELECT '20114' UNION ALL
SELECT '20115' UNION ALL
SELECT '20116' UNION ALL
SELECT '20117' UNION ALL
SELECT '20118' UNION ALL
SELECT '20119' UNION ALL
SELECT '201110' UNION ALL
SELECT '201111' UNION ALL
SELECT '201112' UNION ALL
SELECT '20121'
)
SELECT OriginalData = D
, Converted = CONVERT(CHAR(10),DATEADD(mm,SUBSTRING(D,5,2)-1,SUBSTRING(D,1,4)),101)
FROM cteTestData
;



Results:
OriginalData Converted
------------ ----------
20111 01/01/2011
20112 02/01/2011
20113 03/01/2011
20114 04/01/2011
20115 05/01/2011
20116 06/01/2011
20117 07/01/2011
20118 08/01/2011
20119 09/01/2011
201110 10/01/2011
201111 11/01/2011
201112 12/01/2011
20121 01/01/2012



If you remove the CONVERT, it will be an ANSI solution that returns the DATETIME datatype which is better for the front-end. I wouldn't even think of permanently storing a formatted date in anything but a staging table being made ready for output to a text file.

If it were me, I'd find the person that wrote the code that created original formatted date mess and take them to the woodshed for a bit of education.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7383 Visits: 6431
Jeff Moden (7/24/2013)
savycara (7/24/2013)
DECLARE @d varchar(6) = '20114'
SELECT try_convert(date, substring(@d, 1, 4) +
CASE WHEN len(@d) = 5 THEN '0' ELSE '' END +
substring(@d, 5, 2) + '01')

This worked but I am not getting in 101 style that is 04/01/2011


If you don't mind a non-Ansi solution (and I don't), this is pretty easy. That, not withstanding, it would be good to heed the warnings about formatting dates on the backend.


WITH
cteTestData(D) AS
( --=== This is test data and is not a part of the solution
SELECT '20111' UNION ALL
SELECT '20112' UNION ALL
SELECT '20113' UNION ALL
SELECT '20114' UNION ALL
SELECT '20115' UNION ALL
SELECT '20116' UNION ALL
SELECT '20117' UNION ALL
SELECT '20118' UNION ALL
SELECT '20119' UNION ALL
SELECT '201110' UNION ALL
SELECT '201111' UNION ALL
SELECT '201112' UNION ALL
SELECT '20121'
)
SELECT OriginalData = D
, Converted = CONVERT(CHAR(10),DATEADD(mm,SUBSTRING(D,5,2)-1,SUBSTRING(D,1,4)),101)
FROM cteTestData
;



Results:
OriginalData Converted
------------ ----------
20111 01/01/2011
20112 02/01/2011
20113 03/01/2011
20114 04/01/2011
20115 05/01/2011
20116 06/01/2011
20117 07/01/2011
20118 08/01/2011
20119 09/01/2011
201110 10/01/2011
201111 11/01/2011
201112 12/01/2011
20121 01/01/2012



If you remove the CONVERT, it will be an ANSI solution that returns the DATETIME datatype which is better for the front-end. I wouldn't even think of permanently storing a formatted date in anything but a staging table being made ready for output to a text file.

If it were me, I'd find the person that wrote the code that created original formatted date mess and take them to the woodshed for a bit of education.


Gee whiz! And here I was thinking my STUFF solution was pretty clever.

Nice one Jeff!


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
savycara
savycara
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 13
@Jeff Moden

Thank you..
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87098 Visits: 41113
dwain.c (7/24/2013)[hrGee whiz! And here I was thinking my STUFF solution was pretty clever.

Nice one Jeff!


Thanks, Dwain. Indeed the STUFF solution is clever. I went for the super simple based on a little arcane knowledge that you don't have to select the perfect length string if you want to read to the end of the string. Instead of 5,2 for the substring start and length, I could have just as easily used 5,8000 and it would have produced the same results.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sneumersky
sneumersky
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2494 Visits: 487
If your output requirements are driven by business intelligence or analytic needs, you can always use a lookup table (date dimension) as well. If you are interested, let me know, but I think these guys have the issue covered pretty well :-)
karanraju2k13
karanraju2k13
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 3
Your LOGO is Fantastic how did u do || get that
please
thanks.
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