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 |
gagani
Posting Yak Master
112 Posts |
Posted - 2013-05-30 : 11:02:01
|
I am getting the error: 'Conversion failed when converting the varchar value 'R' to data type int' when I try to run the following queryselect case when 0 between startmonth and endmonth then 'R' else 0 end as units0, case when 1 between startmonth and endmonth then 'R' else 0 end as units1, case when 2 between startmonth and endmonth then 'R' else 0 end as units2, case when 3 between startmonth and endmonth then 'R' else 0 end as units3, case when 4 between startmonth and endmonth then 'R' else 0 end as units4, case when 5 between startmonth and endmonth then 'R' else 0 end as units5, case when 6 between startmonth and endmonth then 'R' else 0 end as units6, case when 7 between startmonth and endmonth then 'R' else 0 end as units7, case when 8 between startmonth and endmonth then 'R' else 0 end as units8, case when 9 between startmonth and endmonth then 'R' else 0 end as units9, case when 10 between startmonth and endmonth then 'R' else 0 end as units10, case when 11 between startmonth and endmonth then 'R' else 0 end as units11from tbl_ipl where iplyear = 2013 and iplbandid = 65 and pupilid = 622347startmonth and endmonth in the above query are integer types, so it's throwing the errorBut I am in need of the following outputunits0 units1 units2 units3 units4 units5 units6 units7 units8 R R R R R R 0 0 0units9 units10 units110 0 0Could anyone make changes to the query for the required output |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-30 : 11:10:01
|
Put all the zeros in the "ELSE 0" in single quotes:SELECT CASE WHEN 0 BETWEEN startmonth AND endmonth THEN 'R' ELSE '0' END AS units0 , CASE WHEN 1 BETWEEN startmonth AND endmonth THEN 'R' ELSE '0' END AS units1 ,.... |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-05-30 : 11:14:52
|
Replace "Else 0" with --> Else '0'0 needs to be in single qoutes '0'CheersMIK |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-05-30 : 11:15:39
|
:)CheersMIK |
 |
|
prett
Posting Yak Master
212 Posts |
Posted - 2013-05-30 : 23:32:44
|
Please follow this discussion to fix this issue:http://www.sqlservercentral.com/Forums/Topic1033821-8-1.aspx#bm1397014 |
 |
|
|
|
|
|
|