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 2008 Forums
 Transact-SQL (2008)
 How to check for null or empty string

Author  Topic 

emyk
Yak Posting Veteran

57 Posts

Posted - 2013-07-25 : 15:36:05
I am trying to check for a null or empty value for a column (nvarchar 7,null). if null or empty to return a value (1).

So far I tried isnull and Len without any luck.

select isnull(col1,'1') from table1 -- Returns 0 rows.

select CASE WHEN Len(col1) < 0 THEN '1' -- Returns 0 rows

SQL SERVER 2008.

thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-25 : 15:43:59
[code]SELECT * FROM YourTable WHERE col1 IS NULL OR col1 = '';
[/code]This will return even if col1 has just a space (or just multiple spaces).
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 2013-07-25 : 15:54:37
select isnull(col1,'1') from table1 is right, but use number only. Do not use '' unless it is a string field.

select isnull(col1,1) from table1

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-26 : 02:04:47
I prefer COALESCE to IS NULL as its ansi based and also deosnt have issues like this

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -