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

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

Related

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

TSQL SELECT CASE & change second column value

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

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

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

Best way to write union query when dealing with NULL and Empty String values

I have to write a query that performs a union between two tables with similar data. The results need to be distinct. The problem I have is that some fields that should be the same are not when it comes to empty values. Some are indicated as null, and some have empty string values. My question is, is there a better way to perform the following query? (without fixing the actual data to ensure proper defaults are set, etc) Will using the Case When be a big performance hit?
Select
When Column1 = '' Then NULL Else Column1 as [Column1],
When Column2 = '' Then NULL Else Column2 as [Column2]
From TableA
UNION ALL
Select
When Column1 = '' Then NULL Else Column1 as [Column1],
When Column2 = '' Then NULL Else Column2 as [Column2]
From TableB
I don't think it would make any difference in performance, but NULLIF is another way to write this and, IMHO, looks a little cleaner.
Select
NULLIF(Column1, '') as [Column1],
NULLIF(Column2, '') as [Column2]
From TableA
UNION
Select
NULLIF(Column1, '') as [Column1],
NULLIF(Column2, '') as [Column2]
From TableB
Use UNION to remove duplicates - it's slower than UNION ALL for this functionality:
SELECT CASE
WHEN LEN(LTRIM(RTRIM(column1))) = 0 THEN NULL
ELSE column1
END AS column1,
CASE
WHEN LEN(LTRIM(RTRIM(column2))) = 0 THEN NULL
ELSE column2
END AS column2
FROM TableA
UNION
SELECT CASE
WHEN LEN(LTRIM(RTRIM(column1))) = 0 THEN NULL
ELSE column1
END,
CASE
WHEN LEN(LTRIM(RTRIM(column2))) = 0 THEN NULL
ELSE column2
END
FROM TableB
I changed the logic to return NULL if the column value contains any number of spaces and no actual content.
CASE expressions are ANSI, and more customizable than NULLIF/etc syntax.
A Case should perform fine, but IsNull is more natural in this situation. And if you're searching for distinct rows, doing a union instead of a union all will accomplish that (thanks to Jeffrey L Whitledge for pointing this out):
select IsNull(col1, '')
, IsNull(col2, '')
from TableA
union
select IsNull(col1, '')
, IsNull(col2, '')
from TableB
You can keep your manipulation operations separate from the union if you do whatever manipulation you want (substitute NULL for the empty string) in a separate view, then union the views.
You shouldn't have to apply the same manipulation on both sets, though.
If that's the case, union them first, then apply the manipulation to the resulting, unioned set once.
Half as much manipulation code to support that way.