Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query from multiple column

Tags:

sql

sql-server

I have the following table named Fruits.

ID English Spanish German
1  Apple   Applice Apple-
2  Orange  --      --

If the program passes 1 and English, I have to return 'Apple'. How could I write the sql query for that? Thank you.

like image 571
TNA Avatar asked Feb 23 '26 00:02

TNA


1 Answers

select
    ID,
    case @Lang
        when 'English' then English 
        when 'Spanish' then Spanish
    end as Name
from Fruits
where ID = @ID;

or, if you have more than one column to choose, you can use apply so you don't have to write multiple case statements

select
    F.ID,
    N.Name,
    N.Name_Full
from Fruits as F
    outer apply (values
        ('English', F.English, F.English_Full),
        ('Spanish', F.Spanish, F.Spanish_Full)
    ) as N(lang, Name, Name_Full)
where F.ID = @ID and N.lang = @lang
like image 61
Roman Pekar Avatar answered Feb 25 '26 14:02

Roman Pekar