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
Related
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
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
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
I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,
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).