How to check for Is not Null And Is not Empty string in SQL server?

SqlSql Server

Sql Problem Overview


How can we check in a SQL Server WHERE condition whether the column is not null and not the empty string ('')?

Sql Solutions


Solution 1 - Sql

If you only want to match "" as an empty string

WHERE DATALENGTH(COLUMN) > 0 

If you want to count any string consisting entirely of spaces as empty

WHERE COLUMN <> '' 

Both of these will not return NULL values when used in a WHERE clause. As NULL will evaluate as UNKNOWN for these rather than TRUE.

CREATE TABLE T 
  ( 
     C VARCHAR(10) 
  ); 

INSERT INTO T 
VALUES      ('A'), 
            (''),
            ('    '), 
            (NULL); 

SELECT * 
FROM   T 
WHERE  C <> ''

Returns just the single row A. I.e. The rows with NULL or an empty string or a string consisting entirely of spaces are all excluded by this query.

SQL Fiddle

Solution 2 - Sql

WHERE NULLIF(your_column, '') IS NOT NULL

Nowadays (4.5 years on), to make it easier for a human to read, I would just use

WHERE your_column <> ''

While there is a temptation to make the null check explicit...

WHERE your_column <> '' 
      AND your_column IS NOT NULL

...as @Martin Smith demonstrates in the accepted answer, it doesn't really add anything (and I personally shun SQL nulls entirely nowadays, so it wouldn't apply to me anyway!).

Solution 3 - Sql

Coalesce will fold nulls into a default:

COALESCE (fieldName, '') <> ''

Solution 4 - Sql

in basic way

SELECT *
FROM [TableName]
WHERE column_name!='' AND column_name IS NOT NULL

Solution 5 - Sql

You can use either one of these to check null, whitespace and empty strings.

WHERE COLUMN <> '' 

WHERE LEN(COLUMN) > 0

WHERE NULLIF(LTRIM(RTRIM(COLUMN)), '') IS NOT NULL

Solution 6 - Sql

An index friendly way of doing this is:

where (field is not null and field <> '')

If there aren't many rows or this field isn't indexed, you can use:

 where isnull(field,'') <> ''

Solution 7 - Sql

Just check: where value > '' -- not null and not empty

-- COLUMN CONTAINS A VALUE (ie string not null and not empty) :
-- (note: "<>" gives a different result than ">")
select iif(null    > '', 'true', 'false'); -- false (null)
select iif(''      > '', 'true', 'false'); -- false (empty string)
select iif(' '     > '', 'true', 'false'); -- false (space)
select iif('	'  > '', 'true', 'false'); -- false (tab)
select iif('
'                  > '', 'true', 'false'); -- false (newline)
select iif('xxx'   > '', 'true', 'false'); -- true
--
--
-- NOTE - test that tab and newline is processed as expected:
select 'x	x' -- tab
select 'x

x' -- newline

Solution 8 - Sql

For some kind of reason my NULL values where of data length 8. That is why none of the abovementioned seemed to work. If you encounter the same problem, use the following code:

--Check the length of your NULL values
SELECT DATALENGTH(COLUMN) as length_column
FROM your_table

--Filter the length of your NULL values (8 is used as example)
WHERE DATALENGTH(COLUMN) > 8

Solution 9 - Sql

The below statement worked for me.

$sql = "SELECT table_name FROM product_reviews WHERE title > '0'  
and  product='Product name'";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser993935View Question on Stackoverflow
Solution 1 - SqlMartin SmithView Answer on Stackoverflow
Solution 2 - SqlonedaywhenView Answer on Stackoverflow
Solution 3 - SqlAnoop VermaView Answer on Stackoverflow
Solution 4 - SqlSaalim BhoraniyaView Answer on Stackoverflow
Solution 5 - SqlDxTxView Answer on Stackoverflow
Solution 6 - SqlLucView Answer on Stackoverflow
Solution 7 - SqlEllisView Answer on Stackoverflow
Solution 8 - SqlBas de VriesView Answer on Stackoverflow
Solution 9 - SqlAbdul ShaikhView Answer on Stackoverflow