I have a SQL SELECT statement that returns an INTEFER in one of the columns of the result set. Instead of returning the INTEGER, I want to return a STRING from a constant ARRAY using the INTEGER in that column as the index into the ARRAY. For example, I have this SELECT statement that returns an INTEGER:
SELECT iStatus FROM statusTable
Instead of returning the INTEGER iStatus, I want to use the value within the SELECT statement to retrieve a STRING value from an ARRAY.
DECLARE @list varchar (23) = 'APPLE, ORANGE, PEAR, OTHER'
How would I modify the SELECT statement to return the STRING from @List indexed by iStatus?
I have tried the CASE statement, and it works, but I have multiple STRING lookups I have to perform within the same SELECT statement and I would like to find a more elegant solution.
I have tried this, and it does work, but I would like to reduce the number of lines in my SQL statement:
SELECT StringStatus =
CASE
WHEN iStatus = 0 THEN 'Requested'
WHEN iStatus = 1 THEN 'Pending'
WHEN iStatus = 2 THEN 'Ordered'
WHEN iStatus = 3 THEN 'Assigned'
END