2

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.

3
  • 1
    Is the program returning a string that is the sql query? you may want to include the program part in your question. Commented Aug 15, 2013 at 6:31
  • Hi, I just need to return the correct column name , Eg. Select English from Fruits where ID=1 , but if the program passes Spanish the query should be Select Spanish From Fruits, I don't know how to dynamically select the column. Thanks Commented Aug 15, 2013 at 6:36
  • Since you want to query on data such as English or Spanish, that's a good indicator that your table design is wrong. Things that you want to query on ought to be values that exist within rows of data - not in the names of columns. The table ought to be ID, Language, FruitName and have one row for each Language, for each ID. Commented Aug 15, 2013 at 6:40

4 Answers 4

5
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
Sign up to request clarification or add additional context in comments.

Comments

2

Fisrt you should normalize database, to support multilanguage, this means split the table in 2

tables:

  1. Fruit (FruitID, GenericName, etc)
  2. Languages (LanguageID, LanguageName, etc)
  3. FruitTranslations (FruitID, LanguageID, LocalizedName)

then the query will be just a simple query to table FruitTranslations...


If you still want a query for this then you can use Dynamic SQL,

DECLARE @cmd VARCHAR(MAX)
SET @Cmd = 'SELECT ' + @Language + 
           ' FROM Fruits WHERE ID = ''' + CONVERT(VARCHAR, @Id) + ''''
EXEC(@Cmd)

3 Comments

this way is subject to sql injection
I'd rather not use dynamic for this task, especially if @lang is entered by user
@ArabicProgrammer indeed it could be subject to sql injection, this could be avoided 1. if you check that @Language is a valid column against sys.tables inner join sys.columns..., 2 check that the @Languague is just one word, 3. @ID is INT Anyway the main point of my answer was the normalization part.
2

Try this one -

DECLARE 
      @Lang VARCHAR(10) = 'English'
    , @ID INT = 1

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = '
SELECT ' + @Lang + ' 
FROM dbo.Fruits 
WHERE ID = ' + CAST(@ID AS VARCHAR(4))

PRINT @SQL
EXEC sys.sp_executesql @SQL

1 Comment

this way is subject to sql injection
0

you can do that using stored procedures because in stored procedures you can use 'if else' condition. ex:

@ID as int,@Lang as varchar(50)

if @Lang = 'English'
 begin
    select ID,English from Fruits where ID = @ID;
 end
else
  if @Lang = 'Spanish'
 begin
    select ID,Spanish from Fruits where ID = @ID;
 end

...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.