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


Conversion failed when converting datetime from character string


Conversion failed when converting datetime from character string

Author
Message
chloe82402
chloe82402
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 43
Hello there. I'm having an issue that I can't seem to resolve with my date.

I get this error message when I run the below code. Thanks

Msg 241, Level 16, State 1, Line 412
Conversion failed when converting datetime from character string.

DECLARE @MONTH VARCHAR (20)
SET @MONTH = '20100801'

SELECT BASE.MARKET_CD,
BASE.PRODUCT_CD,
BASE.COS_CD,
BASE.SERVICE_YR_MO,
BASE.PAID_YR_MO,
RPT_COL = 'PAID ',
CASE WHEN @MEASURE = 1 THEN SUM(BASE.PAYMENT_AMT)
WHEN SUM(M.MBR_CNT) = 0 THEN 0
ELSE SUM(BASE.PAYMENT_AMT)/SUM(M.MBR_CNT) END AS PAYMENT_AMT

INTO #REPORT_DATA

FROM #DATA BASE
LEFT JOIN #MEMBERSHIP M
ON BASE.MARKET_CD = M.MARKET_CD COLLATE SQL_Latin1_General_CP437_BIN
AND BASE.PRODUCT_CD = M.PRODUCT_CD COLLATE SQL_Latin1_General_CP437_BIN
AND BASE.PAID_YR_MO = M.SERVICE_YR_MO

WHERE

BASE.SERVICE_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-24,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH
AND

BASE.PAID_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-15,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH

GROUP BY BASE.MARKET_CD,
BASE.PRODUCT_CD,
BASE.COS_CD,
BASE.SERVICE_YR_MO,
BASE.PAID_YR_MO
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10160 Visits: 10575
You're setting @Month = '20100801'
Then, in you're doing:
BASE.SERVICE_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-24,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH
AND

BASE.PAID_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-15,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH

Adding the string '01' to '20100801' gives you '2010080101', which is not a valid date.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

R.P.Rozema
R.P.Rozema
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1139 Visits: 1685
Well spotted, WayneS.

Apart from that error in the string manipulation, it's better (for performance and to avoid internationalization issues) not to use string manipulation at all on your datetime values. The functions datediff() and dateadd() were designed to manipulate those values.

To retrieve the first day of the current month you can use:
select dateadd(month, datediff(month, 0, getdate()), 0)


Next month's first day is:
select dateadd(month, datediff(month, 0, getdate()), 1)


Similar, to strip off the time part from any datetime value (= find the beginning of the day, 00:00:00.000 in the time component), use:
select dateadd(day, datediff(day, 0, getdate()), 0)



Plus, you're using between on datetime values. Since between is inclusive, you should better use "where @dtvalue >= @dtFromValue and @dtvalue < @dtToValue" instead of "where @dtValue between @dtValueFrom and @dtValueTo" to avoid incorrectly getting rows for the first day of the next month included in your results.



Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
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