231

I would like to know how to use NULL and an empty string at the same time in a WHERE clause in SQL Server. I need to find records that have either null values or an empty string. Thanks.

asked Mar 27, 2013 at 15:50
3
  • 5
    There's an OR keyword in SQL. Commented Mar 27, 2013 at 15:51
  • 21
    This question does not show any research effort. It is important to do your homework. Tell us what you found and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. FAQ. Commented Mar 27, 2013 at 15:53
  • 1
    possible duplicate of How to select data from MySQL table where a column is NULL (this was a MySQL question, but it's basically the same answer for t-sql) Commented Mar 27, 2013 at 15:53

19 Answers 19

377
Select *
From Table
Where (col is null or col = '')

Or

Select *
From Table
Where IsNull(col, '') = ''
answered Mar 27, 2013 at 15:55
Sign up to request clarification or add additional context in comments.

2 Comments

according to @Bradc 's answer in this stackoverflow.com/questions/799584/…, better to use the first approach.
Also note that the comments say that In MSSQL 2016, there is no execution plan difference between using ISNULL in WHERE as opposed to using OR in WHERE. It's said that they both use the index on 'col' and do an index seek.
71

If you need it in SELECT section can use like this.

SELECT ct.ID, 
 ISNULL(NULLIF(ct.LaunchDate, ''), null) [LaunchDate]
FROM [dbo].[CustomerTable] ct

You can replace the null with your substitution value.

Uwe Keim
40.8k61 gold badges193 silver badges308 bronze badges
answered Dec 22, 2015 at 10:40

1 Comment

Oracle's NVL(var1, 'value') will take care of empty string ' ' replacement. T-SQL's Isnull(var1, 'value') doesn't.
31

You can simply do this:

SELECT *
FROM yourTable
WHERE yourColumn IS NULL OR yourColumn = ''
answered Mar 27, 2013 at 15:54

Comments

26
SELECT *
FROM TableName
WHERE columnNAme IS NULL OR 
 LTRIM(RTRIM(columnName)) = ''
answered Mar 27, 2013 at 15:53

3 Comments

SELECT * FROM TableName WHERE columnNAme IS NULL OR RTRIM(columnName) = ''
As Xavier points out, there is no need to do BOTH ltrim and rtrim if the goal is merely to compare to empty string. REASON: if there is ONLY whitespace, a single trim will remove all of it. For example, you don't care whether the comparison fails because what remains is "abc" or " abc".
Technically the trim functions remove spaces, not whitespace — at least, so says the documentation.
8

To find rows where col is NULL, empty string or whitespace (spaces, tabs):

SELECT *
FROM table
WHERE ISNULL(LTRIM(RTRIM(col)),'')=''

To find rows where col is NOT NULL, empty string or whitespace (spaces, tabs):

SELECT *
FROM table
WHERE ISNULL(LTRIM(RTRIM(col)),'')<>''
Enamul Hassan
5,46323 gold badges45 silver badges61 bronze badges
answered Oct 9, 2015 at 2:30

1 Comment

You don't need to ltrim or rtrim when using <> ''. This condition will also exclude values that are whitespace only.
8

Some sargable methods...

SELECT *
FROM #T
WHERE SomeCol = '' OR SomeCol IS NULL;
SELECT *
FROM #T
WHERE SomeCol = '' 
UNION ALL
SELECT *
FROM #T
WHERE SomeCol IS NULL;
SELECT *
FROM #T
WHERE EXISTS ((SELECT NULL UNION SELECT '') INTERSECT SELECT SomeCol);

And some non-sargable ones...

SELECT *
FROM #T
WHERE IIF(SomeCol <> '',0,1) = 1;
SELECT *
FROM #T
WHERE NULLIF(SomeCol,'') IS NULL;
SELECT *
FROM #T
WHERE ISNULL(SomeCol,'') = '';
answered Dec 8, 2015 at 18:17

Comments

5

my best solution :

 WHERE 
 COALESCE(char_length(fieldValue), 0) = 0

COALESCE returns the first non-null expr in the expression list().

if the fieldValue is null or empty string then: we will return the second element then 0.

so 0 is equal to 0 then this fieldValue is a null or empty string.

in python for exemple:

def coalesce(fieldValue):
 if fieldValue in (null,''):
 return 0

good luck

Zaheer Ahmed
28.7k12 gold badges77 silver badges113 bronze badges
answered Feb 15, 2017 at 13:47

Comments

3
SELECT *
FROM Table
WHERE column like '' or column IS NULL OR LEN(column) = 0
answered Mar 27, 2013 at 15:55

Comments

3

You could use isnull function to get both null and empty values of a text field:

SELECT * FROM myTable
WHERE isnull(my_nullable_text_field,'') = ''
answered Mar 27, 2013 at 15:59

3 Comments

The isnull function literally just checks if the value is null. It will not work if the string is empty(" ").
@Max Pringle. I do not state differently. Also the provided code address the question request. Not sure on your contribution. Suggest an edit instead of downvoting.
will take your advice. I put in a suggested edit now. This where clause only gets the null values not the empty string values. A nullif could be used to get the empty values.
3

This is ugly MSSQL:

CASE WHEN LTRIM(RTRIM(ISNULL([Address1], ''))) <> '' THEN [Address2] ELSE '' END
answered Sep 29, 2015 at 16:37

Comments

2
select 
 isnull(column,'') column, * 
from Table 
Where column = ''
answered Jun 3, 2020 at 8:58

Comments

2

You can use in as another method

Select *
From Table
Where column in(null, '')
answered Apr 30, 2022 at 18:44

Comments

1
--setup
IF OBJECT_ID('tempdb..#T') IS NOT NULL DROP TABLE #T;
CREATE TABLE #T(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, NAME VARCHAR(10))
INSERT INTO #T (Name) VALUES('JOHN'),(''),(NULL);
SELECT * FROM #T
 1 JOHN
 2 -- is empty string
 3 NULL

You can examine '' as NULL by converting it to NULL using NULLIF

--here you set '' to null
UPDATE #T SET NAME = NULLIF(NAME,'')
SELECT * FROM #T 
 1 JOHN
 2 NULL
 3 NULL

or you can examine NULL as '' using SELECT ISNULL(NULL,'')

-- here you set NULL to ''
UPDATE #T SET NAME = ISNULL(NULL,'') WHERE NAME IS NULL
SELECT * FROM #T
1 JOHN
2 -- is empty string
3 -- is empty string
--clean up
DROP TABLE #T
answered Nov 7, 2014 at 14:23

Comments

1

In sproc, you can use the following condition:

DECLARE @USER_ID VARCAHR(15)=NULL --THIS VALUE IS NULL OR EMPTY DON'T MATTER
IF(COALESCE(@USER_ID,'')='')
PRINT 'HUSSAM'
Pang
10.2k146 gold badges87 silver badges126 bronze badges
answered May 11, 2017 at 7:59

Comments

1

by this function:

ALTER FUNCTION [dbo].[isnull](@input nvarchar(50),@ret int = 0)
RETURNS int
AS
BEGIN
 return (case when @input='' then @ret when @input is null then @ret else @input end)
END

and use this:

dbo.isnull(value,0)

answered Aug 5, 2017 at 11:32

Comments

1
SELECT * FROM DBO.AGENDA
WHERE 
 --IF @DT_START IS NULL OR EMPTY
 ( ISNULL( @DT_START,'' ) = '' AND DT_START IS NOT NULL ) -- GET ALL DATE
 OR --ELSE
 ( DT_START >= @DT_START ) --FILTER
-- MORE FILTER
SELECT * FROM DBO.AGENDA
WHERE 
 ( ( ISNULL( @DT_START,'' ) = '' AND DT_START IS NOT NULL ) OR ( DT_START >= @DT_START ) ) 
 AND
 DT_END < GETDATE()
answered Jan 18, 2018 at 20:06

Comments

1

youe check null With IS NULL and string Empty With LEN(RTRIM(LTRIM(Column))) = 0 in

SELECT *
FROM AppInfra.Person
WHERE LEN(RTRIM(LTRIM(NationalCode))) = 0 OR NationalCode IS NULL
answered Aug 13, 2018 at 12:40

Comments

1

If you need it in SELECT section can use like this.

SELECT COALESCE(column1, column2) AS columnName
FROM tabel;

if column1 is null column2 will be return

answered Jan 30, 2024 at 12:38

Comments

1

if you want to fetch empty and null values from one column then this code apply

select columnname2 from tablename
where columnname2 is null or columnname2 = '';

select columnlist from table you want to fetch then where in which column you want to fetch values of null then write columnname is null and also you want to find empty values then write perticular column name then = '' this will show that empty values from perticular column

answered Jan 30, 2024 at 13:14

2 Comments

select columnname from tablename where columnname2 is null or columnname2 = '';
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.