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

Answer from Martin Smith on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
SELECT column_names FROM table_name ... a selection from the Customers table used in the examples: The IS NULL operator is used to test for empty values (NULL values)....
Discussions

How to see non null values?
I have a table which does not allow nulls in ColumnA. When I select * where ColumnA is not null, millions of rows are returned, most of them have no visible value . They may contain 1 or more spaces but how can I know for sure? Thanks, More on forums.sqlteam.com
๐ŸŒ forums.sqlteam.com
0
0
July 8, 2022
How to handle a value which is not null but also not an empty string?
Cast it to binary to see what code points are actually there. More on reddit.com
๐ŸŒ r/SQL
12
5
March 12, 2024
What is a null in sql? is it the same as a empty cell?
NULL, in simplest terms, is the absence of data. Not to be confused with '', an empty string. If you are familiar with other languages, you could think of it as similar to undefined, in that the column has a type defined, but the specific cell has no data, and is more of a place holder for future data. When filtering for NULL, you will need to do things like table.col is null or table.col is not null In your Excel example, it depends on how you import the data, and what the definition of the target table is. The target table may have rules built into it to default the data for certain columns, or reject NULL values from other columns. You could have Null, an empty string, zero, weird dates.. etc. More on reddit.com
๐ŸŒ r/learnSQL
10
13
October 5, 2022
feature comparison - When to use NULL and when to use an empty string? - Database Administrators Stack Exchange
On the other side, when concatenating strings in Oracle. NULL varchars are treated as empty strings. ... When you want to express explicitly, that a value is assigned, you have to use something like ' '. And you have to worry whether trimming not empty results in NULL ยท select case when ltrim(' ') is null then 'null' else 'not null' end from dual ... Now looking at DBMS where '' is not identical to NULL (e.g. SQL... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ home โ€บ topics โ€บ sql server 2008 โ€บ t-sql (ss2k8) โ€บ check the variable is empty or null
Check the Variable is Empty or Null โ€“ SQLServerCentral Forums
September 9, 2014 - Edit: sql code tag messed up my "greater than" symbol. You may do it... sometimes... But it is NOT right. Try this: ... If you want to make sure that your varchar value contains some thingelse than NULL and empty string use simple "NOT EQUAL" eg:
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ database โ€บ other-databases โ€บ nosql-database โ€บ 20.3 โ€บ sqlreferencefornosql โ€บ is-null-and-is-not-null-operators.html
IS NULL and IS NOT NULL Operators
is_null_expression ::= ... is NULL. If the input expression returns more than one item, an error is raised. If the result of the input expression is empty, IS NULL returns false....
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ sql โ€บ is-not-null
SQL IS NOT NULL Condition: Syntax, Usage, and Examples
Use SQL IS NOT NULL in a WHERE clause to filter out records that contain empty or missing values in a column.
๐ŸŒ
SQLTeam
forums.sqlteam.com โ€บ sql server administration
How to see non null values? - SQL Server Administration - SQLTeam.com Forums
July 8, 2022 - I have a table which does not allow nulls in ColumnA. When I select * where ColumnA is not null, millions of rows are returned, most of them have no visible value . They may contain 1 or more spaces but how can I knowโ€ฆ
Find elsewhere
๐ŸŒ
Hightouch
hightouch.com โ€บ sql-dictionary โ€บ sql-is-not-null
SQL IS NOT NULL - Syntax, Use Cases, and Examples | Hightouch
December 29, 2023 - You would use the SQL IS NOT NULL operator when you need to filter data from a table based on whether a column's value is not NULL. This is useful when you want to retrieve records with data present in a specific column or attribute, ensuring ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ mysql โ€บ how-to-check-a-column-is-empty-or-null-in-mysql
How to Check a Column is Empty or Null in MySQL? - GeeksforGeeks
February 2, 2024 - To ascertain if a column is empty or null in SQL, use COALESCE(column, '') or column IS NULL OR column = ''. These queries fetch rows where the specified column contains an empty string or null value, ensuring comprehensive coverage of both ...
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ how to handle a value which is not null but also not an empty string?
r/SQL on Reddit: How to handle a value which is not null but also not an empty string?
March 12, 2024 -

I have a table which contains some values which are not null. I thought it might be an empty sting but it was not an empty string either.

I copied that particular field from the table and tried an update statement to convert it into null but it didn't worked either . Kindly suggest something

๐ŸŒ
Devart
devart.com โ€บ home โ€บ how to โ€บ how to handle null or empty values in sql server
How to Handle Null or Empty Values in SQL Server
December 19, 2024 - Consider the following factors ... filtering might not efficiently use indexes. To avoid issues, use filtered indexes to include only non-NULL or relevant rows (e.g., WHERE Column IS NOT NULL)....
Top answer
1 of 11
94

Let's say that the record comes from a form to gather name and address information. Line 2 of the address will typically be blank if the user doesn't live in apartment. An empty string in this case is perfectly valid. I tend to prefer to use NULL to mean that the value is unknown or not given.

I don't believe the physical storage difference is worth worrying about in practice. As database administrators, we have much bigger fish to fry!

2 of 11
28

I do not know about MySQL and PostgreSQL, but let me treat this a bit generally.

There is one DBMS namely Oracle which doesn't allow to choose it's users between NULL and ''. This clearly demonstrates that it is not necessary to distinguish between both. There are some annoying consequences:

You set a varchar2 to an empty string like this:

Update mytable set varchar_col = '';

the following leads to the same result

Update mytable set varchar_col = NULL;

But to select the columns where the value is empty or NULL, you have to use

select * from mytable where varchar_col is NULL;

Using

select * from mytable where varchar_col = '';

is syntactically correct, but it never returns a row.

On the other side, when concatenating strings in Oracle. NULL varchars are treated as empty strings.

select NULL || 'abc' from DUAL;

yields abc. Other DBMS would return NULL in these cases.

When you want to express explicitly, that a value is assigned, you have to use something like ' '.

And you have to worry whether trimming not empty results in NULL

select case when ltrim(' ') is null then 'null' else 'not null' end from dual

It does.

Now looking at DBMS where '' is not identical to NULL (e.g. SQL-Server)

Working with '' is generally easier and in most case there is no practical need to distinguish between both. One of the exceptions I know, is when your column represents some setting and you have not empty defaults for them. When you can distinguish between '' and NULL you are able to express that your setting is empty and avoid that the default applies.

๐ŸŒ
W3Schools
w3schools.com โ€บ mysql โ€บ mysql_null_values.asp
MySQL NULL Values - IS NULL and IS NOT NULL
SELECT column_names FROM table_name ... the "Customers" table in the Northwind sample database: The IS NULL operator is used to test for empty values (NULL values)....
๐ŸŒ
Quora
quora.com โ€บ How-can-you-find-all-values-that-are-not-null-using-a-single-query-in-SQL-Server
How to find all values that are not null using a single query in SQL Server - Quora
Answer (1 of 4): We can use the below for single SELECT * FROM user WHERE username IS NOT NULL; We can use the below for multiple SELECT * FROM user WHERE username IS NOT NULL OR email IS NOT NULL OR description IS NOT NULL;
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql server โ€บ how-to-check-a-column-is-empty-or-null-in-sql-server
How to Check a Column is Empty or Null in SQL Server - GeeksforGeeks
January 31, 2024 - In the above sample output, we can see data without any NULL values, meaning records with NULL values are removed from the result output. It is to be noted that empty string value or column with spaces are displayed in the output if it is there and only NULL is filtered out.
๐ŸŒ
SQLBolt
sqlbolt.com โ€บ lesson โ€บ select_queries_with_nulls
SQLBolt - Learn SQL - SQL Lesson 8: A short note on NULLs
Select query with constraints on NULL values SELECT column, another_column, โ€ฆ FROM mytable WHERE column IS/IS NOT NULL AND/OR another_condition AND/OR โ€ฆ; This exercise will be a sort of review of the last few lessons. We're using the same Employees and Buildings table from the last lesson, but we've hired a few more people, who haven't yet been assigned a building. Sorry but the SQLBolt exercises require a more recent browser to run.
๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ tutorials โ€บ sql โ€บ not-null-in-sql
NOT NULL in SQL
June 22, 2023 - NOT Null constraints are often ... table is made or included later in an existing table. ... Not null is a constraint used in SQL that ensures a value is not empty....
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 387709 โ€บ not-null-property-in-sql-server-express-2019
"NOT NULL" property in SQL Server Express 2019 - Microsoft Q&A
May 8, 2021 - For instance, take a column MiddleName in Persons table. If MiddleName is the empty string, we know that this person does not have a middle name. If the value is NULL, we don't know the middle name of this person might be, or if the person has a middle name at all.
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_isnull.asp
W3Schools.com
The COALESCE() function is the preferred standard for handling potential NULL values. The COALESCE() function returns the first non-NULL value in a list of values. The COALESCE() function works in MySQL, SQL Server, and Oracle (not in MS Access).
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_notnull.asp
SQL NOT NULL Constraint
The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.