How do I check if a column is empty or null in mysql

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I have a column in a table which might contain null or empty values. How do I check if a column is empty or null in the rows present in a table.

(e.g. null or '' or '  ' or '      ' and ...)

回答1:

This will select all rows where some_col is NULL or '' (empty string)

SELECT * FROM table WHERE some_col IS NULL OR some_col = '';


回答2:

As defined by the SQL-92 Standard, when comparing two strings of differing widths, the narrower value is right-padded with spaces to make it is same width as the wider value. Therefore, all string values that consist entirely of spaces (including zero spaces) will be deemed to be equal e.g.

'' = ' ' IS TRUE '' = '  ' IS TRUE ' ' = '  ' IS TRUE '  ' = '      ' IS TRUE etc

Therefore, this should work regardless of how many spaces make up the some_col value:

SELECT *    FROM T  WHERE some_col IS NULL         OR some_col = ' ';

or more succinctly:

SELECT *    FROM T  WHERE NULLIF(some_col, ' ') IS NULL;


回答3:

A shorter way to write the condition:

WHERE some_col > ''

Since null > '' produces unknown, this has the effect of filtering out both null and empty strings.



回答4:

You can test whether a column is null or is not null using WHERE col IS NULL or WHERE col IS NOT NULL e.g.

SELECT myCol  FROM MyTable  WHERE MyCol IS NULL 

In your example you have various permutations of white space. You can strip white space using TRIM and you can use COALESCE to default a NULL value (COALESCE will return the first non-null value from the values you suppy.

e.g.

SELECT myCol FROM MyTable WHERE TRIM(COALESCE(MyCol, '') = '' 

This final query will return rows where MyCol is null or is any length of whitespace.

If you can avoid it, it's better not to have a function on a column in the WHERE clause as it makes it difficult to use an index. If you simply want to check if a column is null or empty, you may be better off doing this:

SELECT myCol FROM MyTable WHERE MyCol IS NULL OR MyCol =  '' 

See TRIM COALESCE and IS NULL for more info.

Also Working with null values from the MySQL docs



回答5:

Another method without WHERE, try this..

Will select both Empty and NULL values

SELECT ISNULL(NULLIF(fieldname,''))  FROM tablename


回答6:

try

SELECT 0 IS NULL ,  '' IS NULL , NULL IS NULL  -> 0, 0, 1

or

SELECT ISNULL('  ') , ISNULL( NULL )  -> 0 ,1

Reference



回答7:

I hate messy fields in my databases. If the column might be a blank string or null, I'd rather fix this before doing the select each time, like this:

UPDATE MyTable SET MyColumn=NULL WHERE MyColumn=''; SELECT * FROM MyTable WHERE MyColumn IS NULL

This keeps the data tidy, as long as you don't specifically need to differentiate between NULL and empty for some reason.



回答8:

While checking null or Empty value for a column in my project, I noticed that there are some support concern in various Databases.

Every Database doesn't support TRIM method.

Below is the matrix just to understand the supported methods by different databases.

The TRIM function in SQL is used to remove specified prefix or suffix from a string. The most common pattern being removed is white spaces. This function is called differently in different databases:

  • MySQL: TRIM(), RTRIM(), LTRIM()
  • Oracle: RTRIM(), LTRIM()
  • SQL Server: RTRIM(), LTRIM()

How to Check Empty/Null :-

Below are two different ways according to different Databases-

The syntax for these trim functions are:

  1. Use of Trim to check-

    SELECT FirstName FROM UserDetails WHERE TRIM(LastName) IS NULL

  2. Use of LTRIM & RTRIM to check-

    SELECT FirstName FROM UserDetails WHERE LTRIM(RTRIM(LastName)) IS NULL

Above both ways provide same result just use based on your DataBase support. It Just returns the FirstName from UserDetails table if it has an empty LastName

Hoping this will help you :)



回答9:

If you want to have NULL values presented last when doing an ORDER BY, try this:

SELECT * FROM my_table WHERE NULLIF(some_col, '') IS NULL;


回答10:

This statement is much cleaner and more readable for me:

select * from my_table where ISNULL(NULLIF(some_col, ''));


回答11:

Check for null

$column is null isnull($column)

Check for empty

$column != ""

However, you should always set NOT NULL for column,
mysql optimization can handle only one IS NULL level



回答12:

Either

SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1 from tablename

or

SELECT case when field1 IS NULL or field1 = ''         then 'empty'         else field1    end as field1 from tablename


回答13:

You can also do

SELECT * FROM table WHERE column_name LIKE ''

The inverse being

SELECT * FROM table WHERE column_name NOT LIKE ''


回答14:

SELECT column_name FROM table_name WHERE column_name IN (NULL, '')


回答15:

try this if the datatype are string and row is null

SELECT * FROM table WHERE column_name IS NULL OR column_name = ''

if the the datatype are int or column are 0 then try this

SELECT * FROM table WHERE column_name > = 0


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!