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 2000 Forums
 Transact-SQL (2000)
 If Statement to Replace Empty String with value

Author  Topic 

CreativeNRG
Starting Member

44 Posts

Posted - 2006-04-09 : 14:53:42
Is it possible in my select statement to replace the value of a field if it is an empty string ?

If the column value returns an empty string I would like to replace it with a different string e.g. 'No Answer' ?

Thanks

www.creativenrg.co.uk

nr
SQLTeam MVY

12543 Posts

Posted - 2006-04-09 : 15:02:57
select case when fld = '' then 'No Answer' else fld end ,
...

You might want to cater for null as well.

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

CreativeNRG
Starting Member

44 Posts

Posted - 2006-04-09 : 16:26:15
Thanks for the help.
Is it possible to cater for nulls in the same statement ?

www.creativenrg.co.uk
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2006-04-09 : 16:46:06
Yes - by including ISNULL() in the statement too
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-04-09 : 16:58:30
select case when coalesce(fld, '') = '' then 'No Answer' else fld end

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

X002548
Not Just a Number

15586 Posts

Posted - 2006-04-09 : 17:35:35
Why doesn't

SELECT COALESCE(Col,'No Answer) AS NewCol FROM myTable99

fit the bill?

Oh, empty string....you sure it's an empty string instead of Null



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-04-10 : 01:55:26
"Oh, empty string ...."

There is a toruous solution therefore:

SELECT COALESCE(NullIf(Col, ''),'No Answer') AS NewCol FROM myTable99

Kristen
Go to Top of Page
   

- Advertisement -