How to hide column based on condition - sql

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

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;

i need to use when in sql view

Please its just a simple question , and i did it before but i cant do it right now .
i need to write when in column in sql view to give me value based on another column
as shown in the pic below
i need to make a new column to give me a range of total value from 1000 to 2000 to be ' 1000-2000'
i used to write it as shown
when [total] between '1000' and '2000' then '1000-2000' else 'not'
It should be case when expression
case when [total] between 1000 and 2000 then '1000-2000' else 'not' end
use case when like below and I'd prefer >= and < rather than between
case when total>=1000 and total<=2000 then '1000-2000' else 'not' end

SQL CASE Statement Change the Column name in THEN clause

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

Why does this case statement not work?

Why does this case statement not work? My table is defined as below. I want the case to return true when M = "M" or false if it is not. Something does not seem to look right with this CASE statement but this is the simple case statement. But I thought that could be as many WHEN values as needed but in this cases there is only True or False and it does not appear that there could be anything different.
The input should be all of the M column in the row. I am expecting the output to be one row since I only have one row that has an M value in the column. I am not certain if the case statement should return false in this case.
CASE M = "M <-- is this is what the criteria is based on
I found that this is not the case however from the post below. When i meant that this did not look right I was referring two the fact that the WHEN portion of the code
WHEN true THEN something
just did not look like it normally does.
Code:
select CASE M = "M"
WHEN true THEN "PPP"
WHEN false THEN "False -- Look Again!"
ELSE "Not Found"
END
from Bat;
Image:
New Code:
select *,
CASE
WHEN M = 'M' THEN
Case
when O = 'O' then 'you found a double (MM)' <--Compiles but does not show result
End
WHEN M is null THEN 'False -- Look Again!'
END as 'What is this value'
from Bat;
That fixed the first problem but you can embed case statements N deep I would think so if i search again on O it should return a correct. But the first case returns only one record. So my second case would have to search in the record correct.
The normal way to write this is:
select (CASE WHEN M = 'M' THEN 'PPP'
WHEN M <> 'M' THEN 'False -- Look Again!'
ELSE 'Not Found'
END)
from Bat;
or:
select (CASE WHEN M is null then 'Not Found'
WHEN M = 'M' THEN 'PPP'
ELSE 'False -- Look Again!'
END)
from Bat;
If I had to guess, your version has unexpected behavior for NULL values, but it is hard to say without knowing what you expect or what it is doing.
Change it like this
select CASE
WHEN M = 'M' THEN 'PPP'
WHEN M is null THEN 'False -- Look Again!'
ELSE "Not Found"
END
from Bat;

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