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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date Conversion error: out of range

Author  Topic 

stahorse
Yak Posting Veteran

86 Posts

Posted - 2013-12-09 : 03:39:13
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

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-09 : 04:13:04
take style 103 instead of 112.

see here: http://msdn.microsoft.com/de-de/library/ms187928.aspx


Too old to Rock'n'Roll too young to die.
Go to Top of Page

stahorse
Yak Posting Veteran

86 Posts

Posted - 2013-12-09 : 04:23:28
quote:
Originally posted by webfred

take style 103 instead of 112.

see here: http://msdn.microsoft.com/de-de/library/ms187928.aspx


Too old to Rock'n'Roll too young to die.



Same error
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-09 : 04:31:37
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'+@dt

SELECT CONVERT(DATETIME, '01' + @dt, 104) as [date]




Too old to Rock'n'Roll too young to die.
Go to Top of Page

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


see
http://visakhm.blogspot.com/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-12-09 : 12:35:52
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:
SELECT 
DATEADD(MONTH, DATEDIFF(MONTH, 0, SYSDATETIME()), 0),
CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, SYSDATETIME()), 0) AS DATE)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-12-10 : 00:40:44
CASTing to varchar will change date value specific to current language. You should always use DATEADD and DATEDIFF function as shown by Lamprey

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -