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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With