Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

SQL Case statement not working properly

$
0
0
I'm using MS Sql Server 2005. It has been awhile since I've needed to write case statements but I didn't think I had forgotten that much. I have a table with a bunch of "rank" codes corresponding to individual line items, and I need to decompose these ranks (currently varchar50) into integers. So I built this statement within a view:

CASE
WHEN theRank = 'A1' THEN 1
WHEN theRank = 'A2' THEN 2
WHEN theRank = 'A3' THEN 3
WHEN theRank = 'A4' THEN 4
WHEN theRank = 'A5' THEN 5
WHEN theRank = 'A6' THEN 6
WHEN theRank = 'A7' THEN 7
WHEN theRank = 'A8' THEN 8
WHEN theRank = 'A9' THEN 9
WHEN SUBSTRING(theRank,1,1) = 'O' THEN 10
ELSE 0
END AS theRankN,



The problem is that on any number of the individual records, the value assigned is flat-out wrong. For example, I tested the substring function completely standalone, and it correctly pulls out the 'O' But then the statement assigns a 4. Similarly, 'A4' sometimes returns a 10.

Can anyone provide some ideas on what I've got wrong?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>