Gopal
Gopal

Reputation: 11982

Finding the null column values

Using SQL Server 2005

I want to find a null column value, if it is null then i have to show as empty otherwise i have to show a column values

Table1

Column1

Abcd
null
efgh
lkmn
null
...
...

Tried Query

Select column1, case when column1 = null then 'empty' else column1 end as status from table1

Select column1, case when column1 = '' then 'empty' else column1 end as status from table1

The above query is not working.

Expected Output

Column1 status

Abcd Abcd
null empty
efgh efgh
lkmn lkmn
null empty
... ...
... ...

How to make a query for the above condition.

Upvotes: 0

Views: 132

Answers (2)

John Hartsock
John Hartsock

Reputation: 86872

You can use isNull(columnName, '') Give it a try

Select 
  column1, 
  isnull(column1, 'empty') as status 
from table1

Upvotes: 3

Dave Markle
Dave Markle

Reputation: 97691

use IS NULL instead of = null:

Select column1, case when column1 IS null then 'empty' else column1 end as status from table1

Upvotes: 2

Related Questions