sqldatabase

SQL replace colum value with a specific string


I need to replace the result of a column with other string values.

In particular, I have an enum:

A1 -> started
A2 -> running

In my database, I only save:

A1
B2

So, my query returns the column including strings such as:

A1
B2

I'd like obtaining:

started
running

Is it possible with a query?

Thank you so much!


Solution

  • You can do it with the CASE expression:

    SELECT ...
    ,      CASE Column
             WHEN 'A1' THEN 'started'
             WHEN 'B2' THEN 'running'
             ... END AS Status
    FROM Table
    

    Read https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15