views:

1451

answers:

6

I've got some blank values in my table, and I can't seem to catch them in an IF statement.

I've tried

IF @value = '' and if @value = NULL and neither one catches the blank values. Is there any way to test whether or not a varchar is entirely whitespace?

AHA! Turns out I was testing for null wrong. Thanks.

+1  A: 

if length(@value) = 0 or @value is null

Kieveli
+2  A: 

(LTRIM(RTRIM(@Value))='' should do the trick.

no_one
+6  A: 

To compare with NULL, use the IS NULL keyword.

--Generic example:
SELECT *
FROM MY_TABLE
WHERE SOME_FIELD IS NULL;

--Instead of    

SELECT *
FROM MY_TABLE
WHERE SOME_FIELD = NULL;
JosephStyons
Or SET ANSI NULLS OFF - I'd prefer to follow the SQL-92 spec tho' and us IS NULL
stephbu
A: 

You may have fields with multiple spaces (' ') so you'll get better results if you trim that:

where ltrim(yourcolumnname) = ''
Bernhard Hofmann
+2  A: 
ltrim(rtrim(isNull(@value,''))) = ''
+1  A: 

where length(rtrim(ltrim(yourcolumnname))) = 0 OR yourcolumnname is null

garykindel