Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 case...when...else

Author  Topic 

V.V.
Starting Member

32 Posts

Posted - 2011-04-02 : 03:20:11
Hi guys,
I have a table which contains NULL among other data. So, in this case I want to replace NULL with "-" and leave the others data intact. For that I used this:

SELECT a,b,c,
CASE
WHEN d IS NULL THEN '-'
ELSE (what I should put here to leave the others data intact?)
END AS e
FROM X.dbo.Table1

L.E.: I think I found a solution after all. Instead script above I use:
SELECT a,b,c,ISNULL(d,e)
FROM X.dbo.Table1

Is it ok with second solution or is a better one?
Thanks.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-02 : 05:20:05
SELECT a,b,c,
CASE
WHEN d IS NULL THEN '-'
ELSE d END AS e
FROM X.dbo.Table1

or just
coalesce(d,'-')
In this case. Coalesce returns the first non-null value in the list.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

V.V.
Starting Member

32 Posts

Posted - 2011-04-02 : 11:54:48
I used second method from my post, the one with ISNULL.

Is ok guys or should I use something else?

@nigelrivett, thanks for help.

Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2011-04-02 : 12:18:59
You should read up on the difference between ISNULL and COALESCE. They are similar, but it is where they are different that could cause you problems.

In your scenario, either will do exactly the same thing. COALESCE is ANSI standard and allows for multiple parameters whereas ISNULL is not standard and only allows a single parameter.

Jeff

Jeff
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-04-05 : 07:06:50
Also refer this
http://beyondrelational.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -