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 2005 Forums
 Transact-SQL (2005)
 Msg 241, Level 16, State 1, Line 8

Author  Topic 

lueylump
Starting Member

25 Posts

Posted - 2011-01-25 : 14:10:06
I have a situation where I am getting the following error:
Msg 241, Level 16, State 1, Line 8
Conversion failed when converting datetime from character string.

The query worked fine before the CASE statement was added so any assistance is appreciated (see code below)!!!


Go

Declare @Previous_Start_Date datetime,
@Previous_End_Date datetime,
@Current_Start_Date datetime,
@Current_End_Date datetime

Set @Previous_Start_Date = '20090101'
Set @Previous_End_Date = '200901231 23:59:59'
Set @Current_Start_Date = '20100701'
Set @Current_End_Date = '20101231 23:59:59'

SELECT A.Group_Formal_Name, A.Group_Informal_Name, A.Group_Code, E.LUV_Desc,
H.Process_Date, H.Batch_Amount, F.Allocation_Amt,
CASE WHEN F.Fund_ID = '32966552-A5C5-4FC4-A001-4BE0B968DB15' THEN 'Hope Gift' ELSE 'General' END AS Fund,
CASE WHEN H.Process_Date between @Previous_Start_Date and @Previous_End_Date THEN 'Y' ELSE 'N' END AS CY2009,
CASE WHEN H.Process_Date between @Current_Start_Date and @Current_End_Date THEN 'Y' ELSE 'N' END AS CalYr
FROM Per_Group_Loc A
INNER JOIN Payment_Device B ON B.Payor_Code = A.Group_Code
INNER JOIN Payment_Device_History C ON C.Pay_Device_ID = B.Pay_Device_ID
INNER JOIN Payment D ON D.Payment_ID = C.Payment_ID
INNER JOIN Payment_Allocation F ON F.Payment_ID = D.Payment_ID
INNER JOIN Payment_Batch H ON H.Batch_ID = D.Batch_ID and H.Process_Date between @Previous_Start_Date and @Current_End_Date
INNER JOIN LookUpValues E ON E.LUV_ID = A.Group_Type_Code

WHERE F.Allocation_Amt > 0 AND
A.Group_Code <> '50067550' and
F.Fund_ID IN ('32966552-A5C5-4FC4-A001-4BE0B968DB15','FE5DCA9C-2B8C-4DFE-8805-EA8604EDA19A')

ORDER BY A.Group_Formal_Name



Phil

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-25 : 14:12:07
Add a CONVERT to the datetime variables to avoid the error.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

lueylump
Starting Member

25 Posts

Posted - 2011-01-25 : 15:00:36
Hey Tara,

Thanks for the response. I am having trouble figuring out where to put the convert statement. And do I just put it on the local variables?

Sorry for being so dense!!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-25 : 15:02:28
You put it inside the CASE. Actually your date column likely needs the convert.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

lueylump
Starting Member

25 Posts

Posted - 2011-01-25 : 15:21:26
Tara, thanks for your help, YOU ROCK!!!

I had to define the local variables as VARCHAR as well as use converts but it ultimately worked!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-25 : 15:27:59
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -