Author |
Topic |
sql4us
Starting Member
24 Posts |
Posted - 2011-09-26 : 12:05:33
|
Hi,I used case statement shown below, but it gives me wrong values..do i need to cast it? Any help is appreciated CASE WHEN K.[test] = 'N/A' THEN '0' WHEN ((K.[test]>='0.0') and (K.[test]<'20.0')) THEN '1' WHEN ((K.[test]>='20.0')and (K.[test]<'35.0')) THEN '2' WHEN (K.[test]>='35.0') THEN '3' ELSE ' ' END AS [Key]Result:test KeyN/A 06.5 3 // supposed to be 1, showing as 38.6 32.1 10.0 18.3 360.0 33.6 24.2 3 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 12:09:31
|
thats because you're comparing them as varchar values. why not do integer comparison?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-09-26 : 12:14:48
|
Also why are you mixing varchar values with decimals?MadhivananFailing to plan is Planning to fail |
 |
|
sql4us
Starting Member
24 Posts |
Posted - 2011-09-26 : 12:18:20
|
quote: Originally posted by madhivanan Also why are you mixing varchar values with decimals?MadhivananFailing to plan is Planning to fail
My results(test) are in decimals (with %) , so I need to assign key values for each of those based on the range.. |
 |
|
sql4us
Starting Member
24 Posts |
Posted - 2011-09-26 : 12:19:44
|
quote: Originally posted by visakh16 thats because you're comparing them as varchar values. why not do integer comparison?Thanks for the reply. My results are in decimal values, is there any sample to show the comparison in integer?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 12:21:50
|
quote: Originally posted by sql4us
quote: Originally posted by madhivanan Also why are you mixing varchar values with decimals?MadhivananFailing to plan is Planning to fail
My results(test) are in decimals (with %) , so I need to assign key values for each of those based on the range..
why storing them with % signs? why cant you store them as numeric values? you can always add formatting (%) at front end application. else you're making the manipulations using values difficult just like what you've experienced------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 12:26:36
|
or else you've do a lot of casts like belowSELECT .....CASE WHEN K.[test] = 'N/A' THEN 0WHEN ((K.[test]*1>=0.0) and (K.[test]*1<20.0)) THEN 1WHEN ((K.[test]*1>=20.0)and (K.[test]<35.0)) THEN 2WHEN (K.[test]*1>=35.0) THEN 3ELSE NULLEND AS [Key]. ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sql4us
Starting Member
24 Posts |
Posted - 2011-09-26 : 12:27:28
|
quote: Originally posted by visakh16
quote: Originally posted by sql4us
quote: Originally posted by madhivanan Also why are you mixing varchar values with decimals?MadhivananFailing to plan is Planning to fail
My results(test) are in decimals (with %) , so I need to assign key values for each of those based on the range..
why storing them with % signs? why cant you store them as numeric values? you can always add formatting (%) at front end application. else you're making the manipulations using values difficult just like what you've experienced------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yep, I did have the % at the front end application, its just the values are in decimals and when I did converted to decimal it still throwing me an error..CASE WHEN K.[test] = 'N/A' THEN '0'WHEN ((CONVERT(decimal(19,2),K.[test])>=0.0) and (CONVERT(decimal(19,2),K.[test])<20.0)) THEN '1'WHEN ((CONVERT(decimal(19,2),K.[test])>=20.0)and (CONVERT(decimal(19,2),K.[test])<35.0)) THEN '2'WHEN (CONVERT(decimal(19,2),K.[test])>=35.0) THEN '3'END AS [Key]Error:Error converting data type varchar to numeric. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 13:04:12
|
thats because you've non numeric values also(N/A) convert it to something else and then try likeSELECT CASE WHEN K.[test] IS NULL THEN 0WHEN ((CONVERT(decimal(19,2),K.[test])>=0.0) and (CONVERT(decimal(19,2),K.[test])<20.0)) THEN 1WHEN ((CONVERT(decimal(19,2),K.[test])>=20.0)and (CONVERT(decimal(19,2),K.[test])<35.0)) THEN 2WHEN (CONVERT(decimal(19,2),K.[test])>=35.0) THEN 3END AS [Key],...FROM (SELECT NULLIF(test,'N/A') AS test...)t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|