Reputation: 2245
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.
Upvotes: 0
Views: 1250
Reputation: 1269763
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
Upvotes: 1
Reputation: 246493
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?
Upvotes: 2