225

In MySQL, can I select columns only where something exists?

For example, I have the following query:

select phone, phone2
from jewishyellow.users
where phone like '813%'
and phone2

I'm trying to select only the rows where phone starts with 813 and phone2 has something in it.

2
  • 4
    Can you clarify what you mean by "phone2 has something in it?" People are taking guesses as to whether you mean phone2 IS NOT NULL, is not blank, is not exclusively whitespace, etc.
    – pilcrow
    Commented Dec 8, 2009 at 20:07
  • 1
    if "something" exists, it obviously is not null.
    – mike
    Commented Dec 8, 2009 at 23:26

13 Answers 13

344

Compare value of phone2 with empty string:

select phone, phone2 
from jewishyellow.users 
where phone like '813%' and phone2<>''

Note that NULL value is interpreted as false.

7
  • 5
    Single quotes, not double quotes.
    – OMG Ponies
    Commented Dec 8, 2009 at 19:33
  • 3
    phone2<>"" will not make it past any SQL syntax checker.
    – OMG Ponies
    Commented Dec 8, 2009 at 20:14
  • 8
    @OMG Ponies: But it works in mysql. This is the only checker that's matter Commented Dec 8, 2009 at 20:44
  • 7
    @ivan: News to me, sorry about that.
    – OMG Ponies
    Commented Dec 8, 2009 at 21:16
  • 7
    <> is same as != . it was simple. but I didn't know :)
    – MFarooqi
    Commented May 9, 2017 at 23:09
53

To check if field is NULL use IS NULL, IS NOT NULL operators.

MySql reference http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html

2
  • 20
    OP is asking about only rows that have something in the phone2 field. IS NOT NULL returns everything that literally is not set to NULL, even the empty string "". !='', or <>'' as Ivan said in the accepted answer, will not return empty strings or NULL values. In other words IS NOT NULL is a subset of !='', but they are not equivalent.
    – None
    Commented Mar 15, 2013 at 1:30
  • NULL is not empty. Commented Jan 13, 2023 at 9:27
47

UPDATE 2024, MySQL 8 - welp, I guess this behavior changed for some reason and it doesn't actually work anymore.

SELECT if(NULL > '','true','false');-- false
SELECT if('' > '','true','false');-- false
SELECT if(' ' > '','true','false');-- true
SELECT if('\n' > '','true','false');-- true
SELECT if('\t' > '','true','false');-- true
SELECT if('Yeet' > '','true','false');-- true

An answer that I've been using that has been working for me quite well that I didn't already see here (this question is very old, so it may have not worked then) is actually

SELECT t.phone, 
       t.phone2 
  FROM jewishyellow.users t
 WHERE t.phone LIKE '813%' 
   AND t.phone2 > ''

Notice the > '' part, which will check if the value is not null, and if the value isn't just whitespace or blank.

Basically, if the field has something in it other than whitespace or NULL, it is true. It's also super short, so it's easy to write, and another plus over the COALESCE() and IFNULL() functions is that this is index friendly, since you're not comparing the output of a function on a field to anything.

Test cases:

SELECT if(NULL > '','true','false');-- false
SELECT if('' > '','true','false');-- false
SELECT if(' ' > '','true','false');-- false
SELECT if('\n' > '','true','false');-- false
SELECT if('\t' > '','true','false');-- false
SELECT if('Yeet' > '','true','false');-- true

UPDATE There is a caveat to this that I didn't expect, but numerical values that are zero or below are not greater than a blank string, so if you're dealing with numbers that can be zero or negative then DO NOT DO THIS, it bit me very recently and was very difficult to debug :(

If you're using strings (char, varchar, text, etc.), then this will be perfectly be fine, just be careful with numerics.

4
  • 1
    This is impressive. Short, no need for function calls, and gets the job done. Thank you. Commented Dec 24, 2020 at 20:44
  • Quick side question: is there a similar tip for the opposite condition, i.e. if I need to select something which is empty or NULL? Currently I'm using both = '' and IS NULL at once Commented Dec 24, 2020 at 20:58
  • @aexl sadly not as easily, doing not null>'' returns null which is still false from the perspective of limiting rows Commented Jan 11, 2021 at 14:34
  • I'd recommend NOT using this with mariadb. It's a lot slower than the other choices. Or, at least compare performance if it matters to you.
    – ember
    Commented Jun 10, 2021 at 21:05
45

Check for NULL and empty string values:

select phone
, phone2 
from users 
where phone like '813%' 
and trim(coalesce(phone2, '')) <>''

N.B. I think COALESCE() is SQL standard(-ish), whereas ISNULL() is not.

1
  • 3
    You don't need to use the trim function, since in MySQL a string of any number of spaces is equal to another string of any number of spaces, I've written in answer on this in quite some detail stackoverflow.com/a/42723975/728236 Commented Mar 10, 2017 at 17:09
21

If there are spaces in the phone2 field from inadvertant data entry, you can ignore those records with the IFNULL and TRIM functions:

SELECT phone, phone2
FROM jewishyellow.users
WHERE phone LIKE '813%'
    AND TRIM(IFNULL(phone2,'')) <> '';
1
  • 1
    You don't need to use the trim function, since in MySQL a string of any number of spaces is equal to another string of any number of spaces, I've written in answer on this in quite some detail stackoverflow.com/a/42723975/728236 Commented Mar 10, 2017 at 17:04
8

Surprisingly(as nobody else mentioned it before) found that the condition below does the job:

WHERE ORD(field_to_check) > 0 

when we need to exclude both null and empty values. Is anybody aware of downsides of the approach?

1
  • ive been trying for hours to get the syntax correct and this just solved everything thanks!
    – demo7up
    Commented Jul 3, 2020 at 20:54
7
select phone, phone2 from jewishyellow.users 
where phone like '813%' and phone2 is not null
1
  • 8
    Note that there is a difference between a NULL value and an empty string '' which is a value. If you want to check against an empty string, use column <> '' as suggested by the other answers.
    – Dzhuneyt
    Commented Jul 31, 2013 at 14:28
7
SELECT phone, phone2 
FROM jewishyellow.users 
WHERE phone like '813%' and (phone2 <> "");

May need some tweakage depending on what your default value is. If you allowed Null fill, then you can do "Not NULL" instead, which is obviously better.

2
  • I won't mark you down but: 1) There is no NOT() function in MySQL 2) phone2="" will not make it past any SQL syntax checker.
    – OMG Ponies
    Commented Dec 8, 2009 at 20:16
  • 1
    Heh, sorry. I use a lot of different SQL servers. Commented Dec 8, 2009 at 22:12
4

Another alternative is to look specifically at the CHAR_LENGTH of the column values. (not to be confused with LENGTH)

Using a criteria where the character length is greater than 0, will avoid false positives when the column values can be falsey, such as in the event of an integer column with a value of 0 or NULL. Behaving more consistently across varying data-types.

Which results in any value that is at least 1 character long, or otherwise not empty.

Example https://www.db-fiddle.com/f/iQvEhY1SH6wfruAvnmWdj5/1

SELECT phone, phone2
FROM users
WHERE phone LIKE '813%'
AND CHAR_LENGTH(phone2) > 0

Table Data

users
phone (varchar 12) | phone2 (int 10)
"813-123-4567"     | NULL
"813-123-4567"     | 1
"813-123-4567"     | 0

users2
phone (varchar 12) | phone2 (varchar 12)
"813-123-4567"     | NULL
"813-123-4567"     | "1"
"813-123-4567"     | "0"
"813-123-4567"     | ""

CHAR_LENGTH(phone2) > 0 Results (same)

users
813-123-4567       | 1
813-123-4567       | 0

users2
813-123-4567       | 1
813-123-4567       | 0

Alternatives

phone2 <> '' Results (different)

users
813-123-4567       | 1

users2
813-123-4567       | 1
813-123-4567       | 0

phone2 > '' Results (different)

users
813-123-4567       | 1

users2
813-123-4567       | 1
813-123-4567       | 0

COALESCE(phone2, '') <> '' Results (same)
Note: the results differ from phone2 IS NOT NULL AND phone2 <> '' which is not expected

users
813-123-4567       | 1
813-123-4567       | 0

users2
813-123-4567       | 1
813-123-4567       | 0

phone2 IS NOT NULL AND phone2 <> '' Results (different)

users
813-123-4567       | 1

users2
813-123-4567       | 1
813-123-4567       | 0
1
  • I find this is the most intuitive syntax Commented Aug 1, 2020 at 6:57
2

We can use CASE for setting blank value to some char or String. I am using NA as Default string.

SELECT phone,   
CASE WHEN phone2 = '' THEN 'NA' END AS phone2 ELSE ISNULL(phone2,0) 
FROM jewishyellow.users  WHERE phone LIKE '813%'
0

Use:

SELECT t.phone, 
       t.phone2 
  FROM jewishyellow.users t
 WHERE t.phone LIKE '813%' 
   AND t.phone2 IS NOT NULL
2
  • 1
    I noticed that sometimes IS NOT NULL is not applied to string only columns with empty values (''). I don't know why, but just wanted to point it out. It is wiser to use t.phone2 <> '' when checking for empty string columns.
    – Dzhuneyt
    Commented Jun 12, 2013 at 8:22
  • @Dzhuneyt Cause NULL is not the same as empty string. Commented Nov 27, 2023 at 11:21
0

you can use like operator wildcard to achieve this:

SELECT t.phone, 
       t.phone2 
FROM jewishyellow.users t
WHERE t.phone LIKE '813%' 
  AND t.phone2 like '[0-9]';

in this way, you could get all phone2 that have a number prefix.

0

In my case I had a varchar column, both the methods of IS NOT NULL & != '' didn't work, but the following worked for me. Just putting this out here.

SELECT * FROM `db_name` WHERE `column_name` LIKE '%*%'

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