26

How do I check if a column is empty or null using a SQL select statement?

For instance, if I want to check:

select * from UserProfile WHERE PropertydefinitionID in (40, 53) and PropertyValue is null or empty
6
  • i want to know for that PropertyValue, how to chaeck that as i want. Commented Jan 24, 2011 at 9:01
  • Could you show us the table definition? Commented Jan 24, 2011 at 9:06
  • 1
    If I understand you correctly, you wish to SELECT only those UserProfiles that have no PropertyValue set (NULL or no , is that correct? Commented Jan 24, 2011 at 9:09
  • 1
    Is your query for Sybase of Microsoft?
    – pascal
    Commented Jan 24, 2011 at 13:11
  • What does "empty" mean? An empty string, or something else?
    – Pondlife
    Commented Apr 17, 2013 at 17:03

6 Answers 6

47

Does this do what you want?

SELECT *
FROM UserProfile
WHERE PropertydefinitionID in (40, 53)
  AND (    PropertyValue is NULL
        or PropertyValue = '' );
2
  • 14
    This works great. However if you have a situation (like I have) where a column with spaces should be treated as empty then just add LTRIM() and RTRIM(). Using Christian's example, this would be or LTRIM(RTRIM(PropertyValue)) = ''. Commented Jul 9, 2014 at 20:58
  • 2
    Love it. @FumblesWithCode -- pretty good, given your handle! Commented Mar 11, 2015 at 19:07
24

Here's a slightly different way:

SELECT *
FROM UserProfile
WHERE PropertydefinitionID in (40, 53)
  AND (LEN(ISNULL(PropertyValue,'')) = 0)
2
  • assigning empty string when null and determining the length of the column thereafter is great. thanks!
    – OneLazy
    Commented Dec 6, 2016 at 7:27
  • I like this - neatly covers everything including strings that only contain spaces, because LEN(' ') = 0.
    – kh42874
    Commented May 13, 2021 at 9:00
12

If you want blanks and NULLS to be displayed as other text, such as "Uncategorized" you can simply say...

SELECT ISNULL(NULLIF([PropertyValue], ''), 'Uncategorized') FROM UserProfile

The above answers the main question very well. This answer is an extension of that and is of value to readers.

1
  • 1
    NULLIF([PropertyValue], '') is much more concise as well. Commented Mar 28, 2023 at 1:08
9

Here is my preferred way to check for "if null or empty":

SELECT * 
FROM UserProfile 
WHERE PropertydefinitionID in (40, 53) 
AND NULLIF(PropertyValue, '') is null

Since it modifies the search argument (SARG) it might have performance issues because it might not use an existing index on the PropertyValue column.

0

An answer above got me 99% of the way there (thanks Denis Ivin!). For my PHP / MySQL implementation, I needed to change the syntax a little:

SELECT *
FROM UserProfile
WHERE PropertydefinitionID in (40, 53)
AND (LENGTH(IFNULL(PropertyValue,'')) = 0)

LEN becomes LENGTH and ISNULL becomes IFNULL.

0
select isnull(nullif(CAR_OWNER_TEL, ''), 'NULLLLL')  PHONE from TABLE

will replace CAR_OWNER_TEL if empty by NULLLLL (MS SQL)

1
  • 1
    Not useful for the OP but useful for me. The right answer in the wrong place Commented Jan 29, 2021 at 10:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.