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 12»»

Trying to convert varchar to datetime Expand / Collapse
Author
Message
Posted Tuesday, July 23, 2013 3:23 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:36 PM
Points: 14, Visits: 13
I am trying to convert 20114 which is stored as varchar to datetime but I am getting the following error

Syntax error during explicit conversion of VARCHAR value '20114' to a DATETIME field.
Can some one help me in solving this?
Post #1476809
Posted Tuesday, July 23, 2013 3:36 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:04 PM
Points: 801, Visits: 715
And what date do you expect 20114 to convert to? And by which rule?

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1476812
Posted Tuesday, July 23, 2013 5:51 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:36 PM
Points: 14, Visits: 13
I need to convert it to 2011401 and need to use convert..Thank you
Post #1476835
Posted Tuesday, July 23, 2013 6:11 PM


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: Yesterday @ 3:08 PM
Points: 3,545, Visits: 7,659
2011401 does not have a common format for a date. Can you explain which is the year, month and day?


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1476842
Posted Tuesday, July 23, 2013 8:05 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:36 PM
Points: 14, Visits: 13
2011 is year
4 month
01 is day
Post #1476857
Posted Tuesday, July 23, 2013 9:43 PM


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: Yesterday @ 3:08 PM
Points: 3,545, Visits: 7,659
You could try something like this
 DECLARE @cDate varchar(5) = '20114'
SELECT DATEADD( MONTH, RIGHT(@cDate, 1) , CAST( LEFT(@cDate,4) + '0101' as DATE))




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1476866
Posted Tuesday, July 23, 2013 10:37 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:36 PM
Points: 14, Visits: 13
Thank you..and actually it is a column that stores data as varchar and data is like this


start date
------------
20114 ----2011 is year and 4 is month
20113
20112
20105
20106

I need to represent above data as (adding date 01 to all rows)

2011401-- year month and date
2011301
2011201
2010501
2010601
that is I need to add date and convert into DATETIME and 101 style

Post #1476870
Posted Wednesday, July 24, 2013 1:47 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:04 PM
Points: 801, Visits: 715
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')


I assume that 201110 would be 2011-10-01

I added try_convert, since most likely you have values in that column that are illegal.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1476906
Posted Wednesday, July 24, 2013 3:10 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: Yesterday @ 7:13 PM
Points: 3,627, Visits: 5,274
Ignoring the try_convert (which is probably a good idea), I think this may be another way:

SELECT CONVERT(VARCHAR(10), CAST(STUFF('201311',5,0,'-0')+'-01' AS DATETIME), 101)
,CONVERT(VARCHAR(10), CAST(STUFF('20131',5,0,'-0')+'-01' AS DATETIME), 101);





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!
Post #1476935
Posted Wednesday, July 24, 2013 8:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:36 PM
Points: 14, 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 Thank you
Post #1477092
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse