SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date Conversion error: out of range
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stahorse
Yak Posting Veteran

85 Posts

Posted - 12/09/2013 :  03:39:13  Show Profile  Reply with Quote
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

Edited by - stahorse on 12/09/2013 03:52:41

webfred
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 12/09/2013 :  04:13:04  Show Profile  Visit webfred's Homepage  Reply with Quote
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

85 Posts

Posted - 12/09/2013 :  04:23:28  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 12/09/2013 :  04:31:37  Show Profile  Visit webfred's Homepage  Reply with Quote
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

India
52309 Posts

Posted - 12/09/2013 :  06:36:19  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4612 Posts

Posted - 12/09/2013 :  12:35:52  Show Profile  Reply with Quote
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)

Edited by - Lamprey on 12/09/2013 12:36:29
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 12/10/2013 :  00:40:44  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000