views:

19

answers:

1

For example:

In the database we have number representations of fields that correspond to strings in the app. We store the data as an integer for size and speed reasons. During testing we compare what's in the app with SQL statement results. The problem is that we see the numbers in the results but there are strings in the app. There is an enum in the code that defines what those numbers mean in terms of a string. Instead of having to look at the enum everytime I want to compare ... is there a way to make the SQL results show the string in a map that I put in the SQL select statement instead of the integer value that's actually in the database?

+2  A: 

You can recreate your Enum in SQL using a CASE statement like this:

select map,
    case map
        when 123 then 'xyz'
        when 456 then 'abc'
    end as StringMap
from MyTable

Even better is to store the Enum as a lookup table. Then you can just join against it:

select m.map, e.StringMap
from MyTable m
inner join MyLookupTable e on m.map = e.map
RedFilter
If only I could convince the DBA to create more tables. Already tried and not gonna happen ... too much maintenance already I guess :-(
Brian T Hannan
I'm getting syntax errors in SQL Server Management Studio using the case statement ... I did it at the end of all the columns of the Select just before the 'From'
Brian T Hannan
Please post your schema and query.
RedFilter
`case map` should be just `case`
Adam Bernier
Perfect ... I got it! 'identifier_str' = case table_col ... need that '=' character and don't need "as StringMap" at the end. This is SQL Server 2008 btw.
Brian T Hannan