Questions tagged [sql-null]

In the SQL query languages of relational databases, NULL is a special value for "unknown". Use that tag for questions concerning the SQL NULL value. Please also use the "ansi-sql" tag if your question is about the SQL standard or a tag that indicates which database you are using.

In , “NULL” stands for the “unknown” value.

In a , it has a special , so that the following holds:

  • NULL AND X is the same as NULL
  • NULL OR X is the same as X
  • NOT NULL is the same as NULL

Also, most s and , when operating on a NULL (unknown) , will produce a NULL result.

That holds for the in particular, so the X = NULL will not produce TRUE or FALSE, but NULL.

Use x IS NULL to test for NULL-ness and X IS NOT DISTINCT FROM Y to test if X and Y are either both NULL or both not NULL and equal.

database deviates from the SQL standard by treating empty strings as NULL, so don't rely on that if you want to write SQL.

Two guidelines for proper use of NULL:

  • Define your database columns as NOT NULL (which is not the default) wherever possible.

    This improves the quality of your data and makes your queries simpler and consequently faster, since they don't have to deal with the oddities of NULL.

    It is trivial to change a column from to , but not vice versa!

  • Use NULL for unknown values, not for values that are known to be absent, infinite values and the like. That will make your SQL intuitively do the right thing.

    For example, a missing comment had better be an empty string than a NULL, so that string operations work as expected.

    Infinite values are better represented by infinity (if your SQL dialect supports that) or values beyond the normal range, so that comparisons have the intended result.

242 questions
240
votes
6 answers

MySQL, better to insert NULL or empty string?

I have a form on a website which has a lot of different fields. Some of the fields are optional while some are mandatory. In my DB I have a table which holds all these values, is it better practice to insert a NULL value or an empty string into the…
roflwaffle
  • 29,590
  • 21
  • 71
  • 94
85
votes
6 answers

Postgresql SQL: How check boolean field with null and True,False Value?

In my database table I am having one boolean column, which have some rows with False, True and Null. These are the cases I have tried: Case 1: select * from table_name where boolean_column is null; works well. Give the result with all rows having…
Anil Kesariya
  • 1,044
  • 1
  • 7
  • 13
48
votes
9 answers

MySQL NULL or NOT NULL That is The Question?

What is the difference between NULL and NOT NULL? And when should they be used?
mii
  • 601
  • 1
  • 7
  • 8
22
votes
2 answers

Can MySQL automatically convert empty strings to NULL?

There is an excellent SO on MySQL empty strings versus NULL here, MySQL, better to insert NULL or empty string?, however it doesn't take in to account 'uniformity' - i.e. if you want to have just one choice in your tables (i.e. empty string OR…
DatsunBing
  • 8,684
  • 17
  • 87
  • 172
13
votes
6 answers

replace NULL with Blank value or Zero in sql server

I have a temp table and in that one of my column total_amount is of integer type and NOT NULL. While querying data, I received NULL values for total_Amount column. How ever I used following syntax to remove nulls but some how still NULL values…
0537
  • 993
  • 8
  • 15
  • 32
12
votes
4 answers

How to OR null value in Apache Solr query?

The last option to solve this for me was to ask StackOverflow. I am trying to create a Solr query to get documents that have a specific value on one of its fields OR that does not have a value... Theorically, this query should have worked. Here is…
Emre Can Geçer
  • 395
  • 3
  • 4
  • 14
10
votes
2 answers

How to print postgres table output so it can distinguish between empty string and null

is there a way to configure psql or to pass in some parameter when doing query, to print out different value for null and for empty string? Currently it shows empty for both. For example, i have a table with this content: adventure=# select * from…
Kristijan
  • 323
  • 6
  • 11
8
votes
1 answer

Postgres: Update Boolean column with false if column contains null

I have existing table called test with two columns id and utilized Here utilized contains three values in my table null, false and true. Here null is default for utilized I want to update the rows with utilized false where utilized is null so I…
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
6
votes
1 answer

Comparing two potentially NULL values in SQLite query

I have an SQLite query like: SELECT max(e), url, branch FROM ( SELECT max(T1.entry) e, T1.url, T1.branch FROM repo_history T1 WHERE ( SELECT active FROM repos T2 WHERE url = T1.url AND branch =…
SoniEx2
  • 1,864
  • 3
  • 27
  • 40
5
votes
4 answers

How to make LAG() ignore NULLS in SQL Server?

Does anyone know how to replace nulls in a column with a string until it hits a new string then that string replaces all null values below it? I have a column that looks like this Original Column: PAST_DUE_COL 91 or more days pastdue …
Ryan
  • 87
  • 1
  • 1
  • 6
5
votes
2 answers

How to avoid that 0 (zero) int turns into Postgres "null" value and violates "not null" constraint?

In Go, I am unmarshalling/decoding JSON into a struct with an ID field of type int. Then I try to insert this struct into a PostgreSQL database using go-pg with the ID column as the primary key (which has a not-null constraint). The first entry has…
5
votes
4 answers

Postgres unassigned record, is there a way to test for null?

Is there some way to test an unassigned record for null? (Sorry, sqlfiddle doesn't like my DO block.) Thanks. DO $$ DECLARE r record; BEGIN r := null; if r is null -- ERROR: record "r" is not assigned yet then end if; END $$;
Emery Lapinski
  • 1,572
  • 18
  • 26
4
votes
4 answers

In Postgres, how would I retrieve the default value of a column, preferably inline in an insert statement?

Here's my example table: CREATE TABLE IF NOT EXISTS public.cars ( id serial PRIMARY KEY, make varchar(32) not null, model varchar(32), has_automatic_transmission boolean not null default false, created_on_date timestamptz not…
kamii
  • 227
  • 2
  • 9
4
votes
2 answers

How to declare the elements of an array non-nullable?

In the following simple table CREATE TABLE foo ( things VARCHAR ARRAY ); It's possible to insert null as an element of things: INSERT INTO foo VALUES ('{"hi", null, "ho"}'); But I'd like to not allow this. Changing the definition to the…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
4
votes
3 answers

Difference between x = null vs. x IS NULL

In Snowflake, what is the difference between x = NULL and x IS NULL in a condition expression? It seems empirically that x IS NULL is what I want when I want to find rows where some column is blank. I ask because x = NULL is treated as valid syntax…
Marty C.
  • 576
  • 2
  • 7
  • 22
1
2 3
16 17