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
2
votes
1 answer

Postgresql - Compare a string with a null value

I'm a bit puzzled because I believe the answer to this question is fairly simple but I've searched and tried several options and couldn't find the right answer. The database is a PostgreSQL 13.1 I am using an API which sends a JSON object to a…
Pierre H.
  • 107
  • 2
  • 12
2
votes
1 answer

Postgresql concat columns and skip column if column in NULL or is empty string

I am trying to concatenate a number of columns and skip the column if column is NULL OR empty in postgres. For example: SELECT CONCAT(coalesce('a',''), '|',coalesce('b',''), '|',coalesce(NULL,''), '|',coalesce('',''), …
2
votes
1 answer

Snowflake - Querying null values in variant

I would like to filter off records with certain keys within variant having null values variant column structure Column name: specs { "color": 32, "browser": null, "language": "en" ) Want something like below: select * from table where specs:browser…
Syamjith
  • 109
  • 8
2
votes
2 answers

ansi_nulls: some ways to check it don't seem to work

The documentation states that the database ANSI_NULLS flag controls the behavior of comparisons with null on some side. I was checking this stack overflow post to check how to determine (not set) the value of this flag. Interestingly, not all of the…
George Menoutis
  • 6,894
  • 3
  • 19
  • 43
2
votes
2 answers

Setting DateTime variable as NULL in CASE expression

I have two DateTime columns Date1 and Date2. If Date1 is populated, Date2 carries the same value, otherwise Date2 carries its own unique value. What I am trying to do is, if Date1<>NULL, Date2=NULL. I am using a CASE statement within a SELECT…
2
votes
2 answers

NULL NOT IN (Empty_Relation) of SQL query shows different behaviors on different engines

I try a query which test NULL NOT IN Empty_Relation on Postrgresql, Spark and I got different results. select count(*) from (select 1) where null not in (a empty relation) Postgresql outputs 1. The other outputs 0. I understand the NULL behaviour…
Bostonian
  • 615
  • 7
  • 16
2
votes
1 answer

Trying to select the max column school-year

I'm trying to follow the answer from this question. However it's not working for me and I can't see why. SELECT h.* FROM `student-history` h LEFT OUTER JOIN `student-history` h2 ON h.id = h2.id AND h.`school-year` < h2.`school-year` WHERE h2.id IS…
Chud37
  • 4,907
  • 13
  • 64
  • 116
2
votes
2 answers

Postgres regr_slope returning NULL

Can someone please help me understand the circumstances in which regr_slope returns NULL where there are rows in the data set? For example: log=> select * from sb1 order by id, ts; id | elapsed | ts ------+---------+---------------- …
2
votes
4 answers

SQL Server group by absorb null and empty values

I have this data: Id Name amount Comments ------------------------------- 1 n1 421762 Hello 2 n2 421 Bye 3 n2 262 null 4 n2 5127 '' Each name may or may not have extra…
heyNow
  • 866
  • 2
  • 19
  • 42
2
votes
3 answers

BigQuery comparing DATE and TIMESTAMP

Here is my example I am using in MySQL. However, in BigQuery, my OnSite timestamp is a DATE and my Documents timestamp is a TIMESTAMP. BigQuery is having trouble with the below query because I get the message: No matching signature for function…
bryan
  • 8,879
  • 18
  • 83
  • 166
2
votes
3 answers

Select Not Including Nulls when not explicitly stated

When I use Select Distinct Brand_ID from db.ABC WHERE Brand_ID <> 800 I get the returned output: +----------+ | Brand_ID | +----------+ | 100 | | 200 | | 300 | | 400 | | 500 | | 600 | | 700 | | 900…
GregdeLima
  • 404
  • 1
  • 9
  • 27
1
vote
1 answer

Generating a flag with respect to a change in consecutive records within partition

I have a snowflake table (called as original_table) with following fields case_num, code, project_name, sp_id, updated_date. For each case_num the combination of code, project_name, sp_id will be unique. There is another table (called as…
1
vote
1 answer

Understanding how NULL is treated in GROUP BY versus the WHERE clause?

While trying to isolate the 7 rows in the first query below, I got results that, as a novice, I was not expecting. I read over this SQLite document but do not understand why the 7 rows which are NULL are separated in the GROUP BY in the first query,…
Gary
  • 2,393
  • 12
  • 31
1
vote
1 answer

SQL checking equality inside a case statement inside where clause?

Here is the code: WHERE 1=1 AND TU.Auction_I IN (41, 42) AND @StatePickupID = CASE WHEN @StatePickupID IS NOT NULL THEN (UP.TransportStateID = @StatePickupID) END AND @StateDropoffID = CASE WHEN…
CrankySwan
  • 31
  • 6
1
vote
1 answer

SQL - Adding a % sign to NVL output/result set - pictures and code included

I am having trouble adding a percent sign to the output of the NVL() function. If you look in the desired output picture you will see that the commission needs to have a % added to it. I've tried various things but have been unable to get it to…
fice
  • 23
  • 4
1 2
3
16 17