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. Answer from belkarbitterleaf on reddit.com
๐ŸŒ
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

๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ need some knowledge on null and not null
r/SQL on Reddit: Need some knowledge on NULL and NOT NULL
December 22, 2021 -
  • Where and why exactly a null is used?

  • What is exactly null and not null? To my understanding Not null we use when its mandatory to insert some value in that field, also when we give check constraint so by default the column will be not null right?

  • By adding new column through alter method default values are null, so how would I be able to insert values in it and is it right to give not null constraint to that new column while adding through alter method, basically when null and when not null to be used?...

god this is so confusing please help me, ik im asking alot but im really confused

๐ŸŒ
Reddit
reddit.com โ€บ r/sqlserver โ€บ how to replace null with empty string in sql server? isnull() vs coalesce() examples
r/SQLServer on Reddit: How to replace NULL with Empty String in SQL Server? ISNULL() vs COALESCE() Examples
October 4, 2024 - If you are just wanting a blank string instead of a null then use isnull(FieldName,โ€™โ€™). Or you came just default to column to a blank string. Difference between VARCHAR vs CHAR data type in SQL Server? (with Example) ... Ever since upgrading the database to SQL Server 2025, queries have been running noticeably slower. ... Making varchar default to empty string vs.
๐ŸŒ
Reddit
reddit.com โ€บ r/database โ€บ sql handles null strings wrong, i've lost 2k hairs from it
r/Database on Reddit: SQL handles null strings wrong, I've lost 2k hairs from it
August 8, 2023 -

The way SQL handles null strings is just lousy for typical CRUD use (business & administration). Roughly 99.9% of the time what's really needed is for null strings to be processed & treated just like a zero-length string ("blank" for short). Having a function or expression to detect whether it's "null" or not would serve fine when explicit null detection is needed. (Other than maybe JOIN-related expressions, I'd almost never need it.)

For example, under most SQL dialects, if you concatenate strings A, B, and C; and say B is null, the ENTIRE expression is null regardless of what A and C are. I've never ever needed this behavior. And the 1 out of gazillion times you do, the null-checking function/expression can ask. Thus, queries end up cluttered with COALESCE, NVL, etc. de-null-er functions. Repetition of code or code patterns is usually a sign your language or API's are a poor fit for the need.

Further, when you see something like this:

   CREATE TABLE Persons (
     ID int NOT NULL,
     LastName varchar(255) NOT NULL,
     FirstName varchar(255) NOT NULL,
     Age int);

The vast majority of the time you really want is "not blank", or more specifically not to have just "white space". There is a wonderful C# function called "IsNullOrWhiteSpace()" that fits the kind of constraint that's really desired. I wish the standard would be modified to have a "NOT EMPTY" constraint:

   -- What we need & want vast majority of the time
   CREATE TABLE Persons (
     ID int NOT NULL,
     LastName varchar(255) NOT EMPTY, /* Can't be null or just white spaces */
     FirstName varchar(255) NOT EMPTY,
     Age int);

Maybe there's a better word choice than "Empty", but this a starting point for pondering and discussion.

Justifications for the status quo are usually academic gobbledygook. To be frank, people in school too long often lose sight of the practical world. I'll lose reddit score for claiming that, but keep seeing it, and not just with nulls. I'm paid to make my boss happy, not professors.

[Edited.]

๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ sql newbie question about not null
r/SQL on Reddit: SQL Newbie question about NOT NULL
March 26, 2024 -

Hi! Me and my sibling-in-law are just beggining to learn SQL and are about to get in a boot camp that gives you an introductory "exam". We failed it the first time, but weren't told why. This Exam willl change, so we're not looking to have our homework done so to say, we just want to understand what we did wrong in the first try.

And after watching a lot of videos and trying different solutions, we're a bit confused about this schema:

What we can't get a grasp on is what's the use of NOT NULL here? Like, how should we add that to our querys?

We're also a bit lost when it comes to item 10, how should we use "join" here?

Thank you in advance, we're doing our best!

I'll translate all the questions so that there's some context:

The first point was:

"Write an SQL query to show all the products in the table "Productos" with a price higher to $50."
Our answer was:

Select * from productos where Price > 50

Second point was:
"Write an SQL query to obtain the total amount of orders (pedidos) made by an specific client according to his ID"
Our answer was:

Select cliente_ID, count(*) as Pedidos_count
from Pedidos
where cliente_ID= โ€˜NOT NULLโ€™
group by cliente_ID

Third point was:
"Write an SQL query to update the price of a product on the table "Productos""
Our answer was:

Update productos set price = โ€˜Floatโ€™
where nombre = โ€˜Varcharโ€™

Fourth point was:
"Write an SQL query to show the names of the products together with their corresponding categories."
Our answer was:

Select nombre_varchar, categoria_varchar from productos

Fifth point was:
"Write an SQL query to delete all the orders that have an amount lesser than 5."
Our answer was:

Delete from pedidos where quantity < 5

Sixth point was:
"Write an SQL query to calculate the total price of the orders made."
Our answer was:

Select SUM (total_precio) as "total_pedidos_precio"
From Pedidos

Seventh point was:
"Write an SQL query to show the names of the products in ascendant alphabetical order."
Our answer was:

select * from productos
Order by nombre asc

Eighth point was:
"Write an SQL query to show the orders made in a specific date." (fecha means date).
Our answer was:

select * from Pedidos where date (fecha_pedido) = NOT NULL

Ninth point was:
"Write an SQL query to obtain the average of the prices of all the products."
Our answer was:

Select AVG (precio) from Productos

Tenth point was:
"Write an SQL query to show the products together with the total amount of orders made for each one."
We weren't sure about this one, we think we have to use the join clause, but we couldn't agree on how to.

Eleventh point was:
"What's the correct syntax to insert a new record in the table "Usuarios" (Users)"
a) INSERT INTO Usuarios (Nombre, Apellido) VALUES ('John', 'Doe'); (Picked this one)
b) INSERT Usuarios (Nombre, Apellido) VALUES ('John', 'Doe');
c) INSERT VALUES ('John', 'Doe') INTO Usuarios;
d) INSERT INTO Usuarios VALUES ('John', 'Doe');

Twelfth point was:
"What's the function used to obtain the total amount of records in a table?"
a) COUNT() (Picked this one)
b) SUM()
c) AVG()
d) MAX()

Thirteenth point was:
"What's the clause used to filter results in a SELECT query?"
a) WHERE (Picked this one)
b) FROM
c) ORDER BY
d) GROUP BY

Fourteenth point was:
"What's the operator used to combine conditions in a WHERE clause?"
a) OR
b) AND (Picked this one)
c) NOT
d) XOR

Fifteenth point was:
"What's the SQL query to delete an existing table?"
a) DELETE TABLE name_table; (Picked this one)
b) DROP name_table;
c) REMOVE name_table;
d) ERASE name_table;

Top answer
1 of 5
19
NOT NULL in the schema context means that the data in the table must contain a value for that column.
2 of 5
5
Hi, In general, NOT NULL in a column defintion, means that all rows must have a value in that column. Try looking into the following point: Point 1: The query looks correct Point 2: The idea looks correct, but the syntax looks is a bit off. As the cliente_ID is NOT NULL according to your diagram, you should not need to test that this is the case. In addition, the correct syntax would be "cliente_ID is NOT NULL" Point 3 : Perhap I am misunderstanding the wording, but this does not look correct. It looks like you have confused updating the datatype of the column with updating the value of a row in the table. It looks like you are trying to update the datatype while the question asks for updating the price value for a specific product. The query should be changed to have a number in "price =" and where statement should be change to use producto_id( In order to update the price for a specific product) Point 4 to 7: They look correct Point 8: Again, it seems like an misunderstanding regarding the NOT NULL constraint. The where statement should be changed to compare against a date. Again( As in Point 2), it should not be necessary to check for NOT NULL, as it is already a part of the table definition, that it is NOT NULL. Point 9: Look correct Point 10: You are correct you should use an join between the two tables. The arrow between the tables indicate which columns you should join on. Producto_ID is the Primary Key( The column which can be used to uniquely identify rows) in productos and it is a Foreign Key( A column that refers to a Primary Key in another table) in the Pedidos table. In order to join the together, you should therefore use Producto_ID in the On clause of your join Point 11 to Point 14: Look correct Point 15: There is a difference between DELETE and DROP. DELETE is used to delete rows in table( Se for example point 5 where you used DELETE along with a filer to remove specific orders). DROP, on the other hand, is used to delete the entire table. Hope that it helps you and gives you better understanding. Happy learning! :)
๐ŸŒ
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)....
Top answer
1 of 11
92

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.

Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/sqlserver โ€บ counting rows where all columns are either null or empty in the row?
r/SQLServer on Reddit: Counting rows where ALL columns are either null or empty in the row?
September 26, 2024 -

I'd rather not write a bunch of AND clauses, so is there a quick, efficient way to do this?

I'm importing some data with 10 fields into a SQL Server from a CSV file. Occasionally this file has null/empty values across all the cells/columns.

What I'd like to do is just write one relatively short sql statement to simply count (at first) all these rows. I'd rather do it without doing something like:

...and (column1 is null or column1 = '')
...and (column2 is null or column2 = '')

etc...

Is there a good way to do this, or am I stuck with the above?

๐ŸŒ
Reddit
reddit.com โ€บ r/sqlserver โ€บ isnull question
r/SQLServer on Reddit: ISNULL question
April 6, 2024 -

I was attempting to write something along the lines of the below to delimit 2 columns if column1 wasn't null.
select iif(ifnull([column1]), '', [column1] + '|') + [column2]

and i settled on something like this in MS SQL Server
select isnull([column1] + '|', '') + [column2]

It seems to work for me as anything concatenated with a NULL is a NULL. I know it limits the output of the second parameter to only 2 character (due to the first parameter being 2 characters), but that's NBD because I'm only outputting an empty string anyway. Anything I'm missing or is this bad form? should I be using case or something else? Thank you

๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ how do you check a list for null values?
r/SQL on Reddit: How do you check a list for NULL values?
October 21, 2019 -

As far as I can tell, the easiest way to check a list of values for a NULL is to concatenate the strings, then check for a null.

SELECT  CASE WHEN 'a' + NULL + 'string' IS NULL THEN 0 ELSE 1 end

Results in 0

SELECT  CASE WHEN 'a' + string' IS NULL THEN 0 ELSE 1 end

Results in 1

If youโ€™re trying to find nulls in a mixed bag (numbers and strings), you can add your numerics, then convert them to a string to concatenate with everything:

SELECT CASE WHEN 'a' +  'string' + CONVERT(varchar(20), 1 + null + 3) IS NULL THEN 0 ELSE 1 END

Results in 0

SELECT CASE WHEN 'a' +  'string' + CONVERT(varchar(20), 1 + 2 + 3) IS NULL THEN 0 ELSE 1 END

Results in 1

Iโ€™m sure you hate this though, but is there an easier way?

๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ is not null problem
r/SQL on Reddit: Is Not Null problem
June 25, 2022 -

Hello,

I've got a database of time stamped events from a youth soccer game.

So far, I have imported the CSV of data into MySQL Workbench.

Here are the datatypes affiliated with the columns:

Here is what my database looks like:

Data, unaltered:

The statement Iโ€™m trying to get to work is:

Select * From game_2_data where Player IS NOT NULL;

All that seems to get me is the original data (inclusive of rows that contain blanks in the Player column).

The following statement does work:

Select * From game_2_data where Player = โ€œJohnโ€;

I assume Iโ€™m overlooking something simple.  Thanks for any pointers!     

๐ŸŒ
Reddit
reddit.com โ€บ r/mssql โ€บ best practices/arguments on empty string vs null
r/MSSQL on Reddit: Best practices/arguments on empty string vs null
January 27, 2021 -

I'm looking for some best practices guides or arguments for/against different designs to solve this problem:

In short: if a string value is optional, make it required and use an empty string, or make it nullable and use null and don't allow empty strings? I assume #1 is the answer but I want to get a feel for what people think, or if there's something new I don't know about.

In full:

I have a server inventory database with some user configuration tables. One table controls expected hostnames across all environments. I have two tables: "HostnameFamily" and "Hostname".

HostnameFamily
- FamilyId [PKEY]
- FamilyName
- (Other Configuration Columns)

HostnameEnvironment
- FamilyId [PKEY]
- EnvironmentName [PKEY]
- Hostname

Through a SQL view this generates a list of all expected hostnames across all environments. Example names are: appserver-dev1, appserver-staging, appserver-production, webserver-dev1, webserver-staging, etc. To make configuration easier and since most follow patterns I allowed * to be set for EnvironmentName and "%env%" in the Hostname to automatically generate names for all environments that didn't have an explicit name, also handled through the view. Not all families have a * entry because some are one-offs for specific environments.

Here's where my question starts. I want to move the * environment pattern out of HostnameEnvironment because I'm expanding the environments this covers greatly and need a foreign key constraint on the EnvironmentName column.

My thought is to add a DefaultPattern column to HostnameFamily, but not all HostnameFamily records have the * pattern so I need to handle this somehow. I assume the preference is to make it required and use an empty string if a default isn't desired? Or is there another preferred way to toggle functionality?

๐ŸŒ
Reddit
reddit.com โ€บ r/mysql โ€บ is there ever a time when you'd prefer empty values of a varchar to be an empty string instead of a null?
r/mysql on Reddit: Is there ever a time when you'd prefer empty values of a varchar to be an empty string instead of a NULL?
May 18, 2020 -

Is there ever a time when you'd prefer empty values of a varchar to be an empty string instead of a NULL?

I was helping someone with an old php project and they decided to just convert all empty strings from the web form into NULL in the database and they modified all fields of all types on all tables to accept NULL.

I don't know enough about databases to say if this was a good or bad idea. What do you think?

๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ question: is/is not null isn't working?
r/SQL on Reddit: Question: IS/IS NOT NULL isn't working?
June 10, 2019 -

I am working on a bunch of orders and I have an order date and the date that the order was "purchased". (The day we processed the order). If we never processed the order because the customer never followed through, there is a blank spot left in the purchased date resulting in a null when I run my query. I attempted to use IS NOT NULL to filter it out in the where statement but nothing happened. Any thoughts?

WHERE
DATE(a.ORDER_DATE) BETWEEN DATE(:startDate) AND DATE(:endDate)
AND
a.PURCH_ORDER_DATE IS NOT NULL;
Result of query

EDIT: For all of those who are wondering, I figured out how to sort out the null values.

SELECT DISTINCT
a.ORDER_NUMBER
,a.SALES_CATEGORY
,a.ORDER_DATE
,a.PURCH_ORDER_DATE
,CASE
    WHEN DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE) >= 0 THEN DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)
    ELSE "null"
 END as Lag
FROM
SOBOOK a

WHERE
DATE(a.ORDER_DATE) BETWEEN DATE(:startDate) AND DATE(:endDate)
AND
ceil(DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)) = DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)

ORDER BY
 ORDER_NUMBER
,Lag