user14038652
user14038652

Reputation: 1

SQL Server - Suggest a query for this scenario

In SQL Server, "I have a field (D1) as 101, 102, 103 in database table: mastersupport.

I have this query

select right(D1, R) 
from mastersupport.

It will return results like 1, 2, 3

But my requirement is that I want to show result as A, B, C only instead of 1,2,3". Please suggest a query.

I tried with the below but got syntax error.

SELECT DISTINCT
    REPLACE(REPLACE((RIGHT(D1, 1)), '1' , ‘A’), '2', ‘B’, ) AS ExtractString 
FROM
    master_support;

Any other query to derive the result as A, B, C ......

Upvotes: 0

Views: 49

Answers (1)

GMB
GMB

Reputation: 222482

You can use a case expression:

select case right(d1, 1)
    when '1' then 'A'
    when '2' then 'B'
    when '3' then 'C'
    ...
end as extract_string
from master_support

Note that if d1 is of a numeric datatype, using arithmetics seems like a more natural approach:

select case d1 % 10
    when 1 then 'A'
    when 2 then 'B'
    when 3 then 'C'
    ...
end extract_string
from master_support

Upvotes: 2

Related Questions