TSQL SELECT CASE & change second column value - sql

I need to change the status of ColumnB depending on the value of ColumnA. Something like pseudocode:
CASE WHEN ColumnA = 'True' THEN ColumnB = 'Alert' ELSE ColumnB
I am using Azure SQL Server.

The pseudo code actually seems quite right. Just drop the assignment to ColumnB and add an end:
SELECT ColumnA,
CASE WHEN ColumnA = 'True' THEN 'Alert' ELSE ColumnB END
FROM MyTable
Also, note you can use a slightly neater syntax (although it's a matter of taste, mostly), since all (of the one) conditions you have are on the same expression:
SELECT ColumnA,
CASE ColumnA WHEN 'True' THEN 'Alert' ELSE ColumnB END
FROM MyTable

Based on your provided example, I don't think you even need a case statement. A simple update will work just fine.
update YourTable
set ColumnB = 'Alert'
where ColumnA = 'True'
If, on the other hand, you are actually updating other columns at the same time based on other conditions, which prevents you from inserting the where ColumnA = 'True' clause, then you can do it with the case statement this way:
update YourTable
set ColumnB = case when ColumnA = 'True' then 'Alert' else ColumnB end,
ColumnX = ...
from YourTable

Related

Create custom column based off of other columns in SQL query (SQL Server)

I'm having a hard time finding correct syntax to do the following:
SELECT
ColumnA,
ColumnB,
ColumnC,
(if Column1 IS Null and Column2 IS NOT NULL) Then 'Pending' Else '' AS ColumnD
I've tried IF/ELSE, IIF(), but I can't seem to get these queries to work.
use case when expression
SELECT ColumnA,ColumnB,ColumnC,
case when Column1 IS Null and Column2 IS NOT NULL Then 'Pending' Else '' end AS ColumnD
from yourtable

Multiple Columns in Case Statement

Hello I am working on a simple case statement in SQL and was wondering if there is a way to search several columns in the when clause.
Something like:
Case
When (columnA,ColumnB,ColumnC,..,ColumnZ) = 'Something' Then 'Yes'
Else No
End
Where 'Something' is in one of those columns.
I think you want in:
When 'Something' in (columnA, ColumnB, ColumnC,.., ColumnZ) Then 'Yes'
Note that this is an or condition, not that all match.
Yes. You have to check the condition for each column
Case
When (columnA = 'Something' OR ColumnB = 'Something' OR ColumnC = 'Something' .... OR ColumnZ = 'Something') Then 'Yes'
Else 'No'
End

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

Sql statement for mutually exclusive events

I am trying to run an sql statement on an iSeries that will output retuls based on a type parameter I pass in.
Just say mytable has a field called field1. field1 contains Y,N and NULL values.
A type of 'Y' should return just 'Y' values.
A type of 'N' should return not 'Y' values. (ie. Null, N and any other junk in the field)
I tried this...
select *
from mytable
where field1 in case when :type = 'Y' then 'Y'
else (select field1 from mytable where field1 <> 'Y') end
However, this does not work.
I believe the logic you are looking for is this:
SELECT *
FROM myTable
WHERE (:type = 'Y' AND field1 IS NOT null AND field1 = 'Y')
OR (:type <> 'Y' AND (field1 IS null OR field1 <> 'Y'))
(keep in mind the fact that short-circuit logic is not garuanteed with SQL...)
Remember that null doesn't really compare to anything, and it's best to call out the fact that you actually want it (in the second case).

Return one of two columns in a view - whichever one is not null

I have a table with three columns:
ColumnA ColumnB ColumnC
AAA NULL 123
BBB 222 NULL
CCC NULL NULL
I would like to create a SELECT statement which will return ColumnA, and then a second column which will either show the value of ColumnB unless ColumnB is null; otherwise it will show the value of ColumnC, even it it's NULL. Can I use an IF statement for that? Something like:
SELECT ColumnA,
IF(ColumnB IS NULL, ColumnC, ColumnB)
FROM table
**If I get this working, the next step would be to return the value of a joined column instead of ColumnB. In effect the IF statement would be
IF(table.ColumnB IS NULL, table.ColumnC, table2.ColumnD)
Use COALESCE
SELECT ColumnA, COALESCE(ColumnB, ColumnC) as 'Value'
Reading to the end of your question it sounds like you need to use CASE
CASE WHEN table.ColumnB IS NULL
THEN table.ColumnC
ELSE table2.ColumnD
END AS Whatever