Search code examples
sqlsql-serversubqueryenumeration

SQL Return Column value as Enum - GetValue() field from another Column


Here are the two tables;

Cathegories

----------------------------------------
Cathegory (tinyint)  |  Name (nvarchar)
----------------------------------------
0                    |  Field
1                    |  Mountain
2                    |  River
----------------------------------------

Places

------------------------------------------
Name (nvarchar)      |  Cathegory(tinyint)
------------------------------------------
Abc                  |  2
Xyz                  |  1
------------------------------------------

When I want to retrieve the Places listing Names and their Cathegories not in the int format but according to the description in Cathegories.

So retrieving Abc I want it like this;

"River" instead the '2'


Solution

  • You need to join two table on cathegory as shown below.

    select
        p.name as places,
        c.name as category_name
    from places p
    join cathegories c
    on p.cathegory = c.cathegory