SQL CASE Statement Change the Column name in THEN clause - sql

I'm beginner in SQL,is it possible to change column name in CASE statement.
I've a column name as "Inbound" the value for it can be '1' or '2'. If it's 1 the column name should be AS "IN" ELSE "OUT".
Any help is greatly appreciated.

You can not generate dynamic column names. Queries should be static. But what you could do is for instance:
select case when inbound = 1 then 'yes' end as in_result,
case when inbound = 2 then 'yes' end as out_result
from your_table

Related

How to update column based on select statement result

I have a table with following columns :
SalaryStructure:
Id StructureName IsApplicable IsActive
IsApplicable - bit, not null
"IsApplicable" is a newly added column which has all the values as "false" by default.
Now, I want to update "IsApplicable" based on pattern matching logic and then set the value of "IsApplicable" based on that.
Query:
SELECT
CASE
WHEN (StructureName LIKE '%Associate1%' OR StructureName Like '%Tier1%')
THEN 'No' ELSE 'Yes' END AS IsApplicable,
FROM SalaryStructure
WHERE IsActive = 0
I want to run above script on same table SalaryStructure and update the values "Yes" and "No" in the column "IsApplicable".
But I am not getting how to include this part in "Update" statement. I want update all the records in the SalaryStructure table.
Can someone please help me?
Neither 'Yes' or 'No' are valid bit values, a non-NULLable bit column can store 1 or 0 and that is it. I therefore assume 'No' should be 0 and 'Yes' should be 1.
As for the UPDATE it would, in truth, look like another other UPDATE on a single table. UPDATE...SET...WHERE:
UPDATE dbo.SalaryStructure
SET IsApplicable = CASE WHEN (StructureName LIKE '%Associate1%' OR StructureName LIKE '%Tier1%') THEN 0
ELSE 1
END
WHERE IsActive = 0;

How to hide column based on condition

Im newbie in sql and i want to filter results of a select statement.I would like to hide the value of specific column in case of a specific value in another column(same row).
For example i want the Products.product column to be hidden or empty in case that the value in the Products.active column is false :
Thanks in advance
SELECT Products.product,Products.Active,
CASE Products.product
WHEN 'false' THEN Products.Active = ''
END
FROM Products
SELECT Products.Active,
CASE Products.Active
WHEN 'false' THEN ''
ELSE Products.product
END AS 'HIDDEN COLUMN'
FROM Products
msdn
You'd probably want to eliminate that entire record using the WHERE clause instead of CASE. If you do want the record and just want this field empty use the following CASE statement:
CASE Products.Active
WHEN false
THEN null
ELSE Products.product
END

Separate sql value from one field

I have this:
**value**
S:581930640 | P:581930640
And I would like to get the value as in Oracle:
**valuaA ValueB**
581930640 581930640
select
case
when field like 's:%'
then substr(field,3,13)
else null
end as A,
case
when field like 's:%'
then substr(field,17)
else null
end as B
from table;
both the case condition are same, but the substring is different, this should do what you are trying to do.

Dynamic Label in Select

I was wondering if we can change the label of the select statement like we do for data in sql select using CASE
SELECT CASE column1 = 1 THEN 1 ELSE 0 END AS [Available]
But can we have a dynamic header something like
SELECT column1 AS <-- Available when 1 or Not Available when 0
This can be handled on the front end but its wise if we have it on backend. Any help or useful link is appreciated
You can do it with dynamic sql and if...else instruction but it not make sense for me. In relational database value in the cell tells you if something is available or not. If header tells you the same as cell it's duplicate information. If you want to description of the value you can use case syntax instead of 0/1 value
SELECT CASE when column1 = 1 THEN 'Available'
ELSE 'Not available'
END AS [Available]
Well, that would not make sense, as what would you expect the column name to be if you hade 2 rows, one with 1 (being available) and the other with 0(being Not Available)?
You would have to stick to something like
SELECT
CASE
WHEN column1 = 1
THEN 'Available'
ELSE 'Not available'
END as Availability
FROM YourTable

SQL Server Inline CASE WHEN ISNULL and multiple checks

I have a column with some nulls. If that column is null, I want to condition output for it based on values in another column.
So if case when null (if c=80 then 'planb'; else if c=90 then 'planc')
How would you code that in an inline T-SQL statement?
thanks.
COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)
You can also use the nested case statement. Assuming that the first column is called DataColumn.
CASE
WHEN DataColumn IS NULL THEN
CASE c
WHEN 80 THEN 'planb'
WHEN 90 THEN 'planc'
ELSE 'no plan'
END
ELSE DataColumn
END