0

I have this query.

Select '1' as "Test" , (CASE WHEN (True) Then '2' ELSE '3' END)

Id   case text
1     2 

I want case text to be Two when true i.e.

   Id    Two
    1     2

And When Sql is

Select '1' as "Test" , (CASE WHEN (False) Then '2' ELSE '3' END)

Id    Three
 1     3

As will not fix my issue

  Select '1' as "Test" , (CASE WHEN (True) Then '2' ELSE '3' END) as Two/Three.

Because at once true or false will be output. When true then I want Two as column name and When false I want three. For now I have given it hard coded.

3
  • 1
    TRUE is always true... Commented Sep 16, 2016 at 9:11
  • I will get true false based on some BI tool parameter. I have just given example above Commented Sep 16, 2016 at 9:12
  • try this remove that slash in between Two/Threee Select '1' as "Test" , (CASE WHEN (True) Then '2' ELSE '3' END) as Two_or_Three @TBAG Commented Sep 16, 2016 at 9:15

2 Answers 2

2

You can't do this with SQL; there's no way to choose a column name based on the table content (what would that be if there were more than one rows in a table and the column values are different?).

What you could do is send a different query to the database for different values of the boolean parameter.

But the idea of choosing a column name based on the contents is weird. Do you really need to do that?

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot do what you want with a simple SQL statement because a SELECT returns a set of columns, fixed in name and number. You could use dynamic SQL to change the name of the column.

However, why not just return two separate columns with flag values, if that is what you want:

Select '1' as "Test",
       (case when . . . then 1 else 0 end) as flag_2,
       (case when not (. . .) then 1 else 0 end) as flag_3

Then look at the value in the column rather than the presence of the column to get the information you want.

Or, put the value in the column:

Select '1' as "Test",
       (case when . . . then '2' else '3' end) as flag

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.