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.
Author |
Topic |
datachick
Starting Member
3 Posts |
Posted - 2014-11-04 : 17:19:33
|
I am having issues when trying to run this query. Not sure if it has to do with the Case statement or not.ERROR MESSAGE:Msg 245, Level 16, State 1, Line 2Conversion failed when converting the varchar value 'Age' to data type int.SELECT table1.CL_ID, table2.EX_ID, table1.C_STAT, table1.C_M_REAS, table1.CL_R_DT, table1.PR_ID, DateDiff(d, table1.CL_R_DT, GETDATE())as Age, CASE WHEN 'Age' < 11 THEN '00 - 10 Days' WHEN ('Age' >= 11 AND 'Age' <= 20) THEN '11 - 20 Days' WHEN ('Age' >= 21 AND 'Age' <= 30) THEN '21 - 30 Days' ELSE '31+ Days' END AS 'AgingBucket'FROM dbo.Table4INNER JOIN ( dbo.table2 INNER JOIN ( dbo.table1 INNER JOIN dbo.table3 ON dbo.table1.M_CK = dbo.table3.M_CK ) ON dbo.table2.CL_ID = dbo.table1.CL_ID ) ON dbo.Table4.BS_CK= dbo.table1.BS_CKWHERE ( ((dbo.table1.C_STAT) IN ('01','11','15')) AND ((dbo.table1.PR_ID) IN ('1078910','1545410', '1243547','0085410') AND((dbo.table2.ID_OR) = 'PD') AND ((dbo.table1.RG_CK) = 33568) AND ((dbo.table3.RG_CK) = 33568) AND ((dbo.Table4.RG_CK) = 33568) GROUP BY table1.CL_ID, table2.EX_ID, table1.C_STAT, table1.C_M_REAS, table1.CL_R_DT, table1.PR_ID, DateDiff(d, table1.CL_R_DT, GETDATE())as Age |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-11-04 : 17:38:15
|
Your case stmt is comparing the string 'Age' to integers which is why kit fails. What do you want to do with the case? Maybe just unquote Age |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2014-11-04 : 18:38:19
|
quote: Originally posted by datachick I am having issues when trying to run this query. Not sure if it has to do with the Case statement or not.ERROR MESSAGE:Msg 245, Level 16, State 1, Line 2Conversion failed when converting the varchar value 'Age' to data type int.SELECT table1.CL_ID, table2.EX_ID, table1.C_STAT, table1.C_M_REAS, table1.CL_R_DT, table1.PR_ID, DateDiff(d, table1.CL_R_DT, GETDATE())as Age, CASE WHEN 'Age'DateDiff(d, table1.CL_R_DT, GETDATE()) < 11 THEN '00 - 10 Days' WHEN ('Age' >= 11 AND 'Age' <= 20)DateDiff(d, table1.CL_R_DT, GETDATE()) < 21 THEN '11 - 20 Days' WHEN ('Age' >= 21 AND 'Age' <= 30)DateDiff(d, table1.CL_R_DT, GETDATE()) < 31 THEN '21 - 30 Days' ELSE '31+ Days' END AS 'AgingBucket'FROM dbo.Table4INNER JOIN ( dbo.table2 INNER JOIN ( dbo.table1 INNER JOIN dbo.table3 ON dbo.table1.M_CK = dbo.table3.M_CK ) ON dbo.table2.CL_ID = dbo.table1.CL_ID ) ON dbo.Table4.BS_CK= dbo.table1.BS_CKWHERE ( ((dbo.table1.C_STAT) IN ('01','11','15')) AND ((dbo.table1.PR_ID) IN ('1078910','1545410', '1243547','0085410') AND((dbo.table2.ID_OR) = 'PD') AND ((dbo.table1.RG_CK) = 33568) AND ((dbo.table3.RG_CK) = 33568) AND ((dbo.Table4.RG_CK) = 33568) GROUP BY table1.CL_ID, table2.EX_ID, table1.C_STAT, table1.C_M_REAS, table1.CL_R_DT, table1.PR_ID, DateDiff(d, table1.CL_R_DT, GETDATE())as Age
|
|
|
datachick
Starting Member
3 Posts |
Posted - 2014-11-05 : 10:26:02
|
Thanks for your suggestions gbritton and bitsmed! bitsmed's recommendation was a success. I am converting from Access sql to t-sql and sometimes the error msgs are misleading when trying to rewrite. |
|
|
|
|
|
|
|