Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have this query DECLARE @dt VARCHAR(20)SET @dt = cast(MONTH(getdate()) as varchar(20)) + cast(YEAR(getdate()) as varchar(20))SELECT CONVERT(DATETIME, '01' + @dt, 112) as [date]I get this error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. please help
This works:DECLARE @dt VARCHAR(20)SET @dt = '.' + right('0' + cast(MONTH(getdate()) as varchar(20)),2) + '.' + right('0000' + cast(YEAR(getdate()) as varchar(20)),4)--select '01'+@dtSELECT CONVERT(DATETIME, '01' + @dt, 104) as [date]Too old to Rock'n'Roll too young to die.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-12-09 : 06:36:19
quote:Originally posted by stahorse I have this query DECLARE @dt VARCHAR(20)SET @dt = cast(MONTH(getdate()) as varchar(20)) + cast(YEAR(getdate()) as varchar(20))SELECT CONVERT(DATETIME, '01' + @dt, 112) as [date]I get this error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. please help
What is the goal of your query? Are you trying to get the 1st of the current month? If so, you maybe you can try date math instead of string manipulation:
CASTing to varchar will change date value specific to current language. You should always use DATEADD and DATEDIFF function as shown by LampreyMadhivananFailing to plan is Planning to fail