Levi Wallach
Levi Wallach

Reputation: 433

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.

Upvotes: 0

Views: 630

Answers (1)

Fahmi
Fahmi

Reputation: 37483

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

Upvotes: 2

Related Questions