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

How to show only values in a column and ignore the blank cells in Access query?

I have a column that has a primary key and then a column that has values in some of them. How do I get the query to show the values in those columns only? I have tried the following and keep getting errors: SELECT [Case Level Report].[Charge Level]…
-1
votes
1 answer

WHERE NOT EQUAL

I have the following SQL statement: select s.conclusion, t.* from trip t JOIN triprequirementsmapping m ON m.tripid = t.trip_id JOIN approvalsubmission s ON s.requested = m.corporatetravelrequesturi where s.conclusion <> 'DISCONTINUED' The value…
Richard
  • 8,193
  • 28
  • 107
  • 228
-1
votes
2 answers

Formatting string if it's not null

Based on GMB's answer on my previous question I've simplified the query: SELECT tn.Date, case when tn.DrControl = 7 then b1.Name || ' -> ' || c1.Name || ' ' || ifnull(l1.Name, '') || ' ' || ifnull(s1.Name, '') || ' ' || ifnull(p1.Name, '') ||…
user6283344
-1
votes
2 answers

sql: select where problem with null value

students table: id name 0 NULL 1 John Query: SELECT * from students WHERE name != "John"; Expected output: 0 NULL With my test, the result is empty, how i can solve this? SELECT * from students WHERE name !=…
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
-1
votes
2 answers

Is comparing against nulls defined behavior?

In a recent code review, I was asked to change Amount > 0 to ISNULL(Amount,0) > 0 Is there a chance these might right different results? From what I can tell, they would both return false if Amount is null or 0 and true in any other case To give…
Kevin
  • 7,162
  • 11
  • 46
  • 70
-1
votes
3 answers

SQL LEFT JOIN behaviour

Trying to get a SQL LEFT JOIN to return NULLs where there are no corresponding rows in the other table. Table 1 - T1 id n 1 aaa 2 bbb 3 ccc Table 2 - T2 t1_id t3_id 1 1 2 1 3 1 1 2 3 2 2 3 3 3 In T2,…
DerekBez
  • 15
  • 4
-1
votes
2 answers

Update or Insert SQL with Powershell NULL values

Trying to figure out how to add a MSSQL-Null value if the value isn't present. This works so far, it will insert new rows and if match it will update the columns. But if for example termniationdate isn't specified, instead of assigning a null value…
SuperDOS
  • 301
  • 1
  • 3
  • 16
-1
votes
2 answers

Selecting columns that are not all null

So I have a query that looks like this - select case when @subType = 1 or @subType = 2 then id end as Id, case when @subType = 3 then name end as name case when @subType = 3 or @subType = 2 then address end as address from table So the issue I…
sam smith
  • 107
  • 1
  • 8
-1
votes
3 answers

Why does SELECT 'foo' + NULL; returns NULL on SQL Server

Why does SELECT 'foo' + NULL; returns NULL on SQL Server? I would expect it to return foo. Update: The SQL query is generated by Entity Framework Core from C# and I expected the null handling behavior to be translated.
mobal
  • 93
  • 5
-2
votes
1 answer

How can I Replace an Artists' Last Name in SQL with the IN Clause?

I am currently learning SQL and I am doing a SQL assignment with the following scenario: Select the FirstName and LastName for Artists who were born in Canada or the UK. Replace any artists last name values who do not have a last name with 'NA' in…
user20166753
-2
votes
2 answers

SQL CASE that matches value or uses LIKE '%'

I'm trying to do a wildcard retrieve if a defined variable is blank. But I can't figure out the correct syntax. SELECT ... FROM ... WHERE customers = CASE WHEN ISNULL(@customerID, '') = '' THEN LIKE '%' ELSE @customerID END
-2
votes
3 answers

Alter table and set default as current date

while creating a table I forgot to add a date of birth column. Is there a way to add a NOT NULL column to a table with default value being current date?
joel
  • 263
  • 1
  • 5
  • 16
-2
votes
1 answer

Null value not showing up

**1. I am having trouble for Null value not showing up I tried with Null defined as === or == and also "" and '' and also I tried using hardcode but still not showing up anything this my output I am getting my output this is the original…
-2
votes
1 answer

error problem with 1582 Incorrect parameter count in the call to native function 'ISNULL'

i have 2 tables one (visits) and other one (patients) with relationship by mysql (patients) is with PK as patients.pid and visits.pid as index and i wanted to use code in my vb.net app to get the last datetime record to check the patient if still…
baders
  • 23
  • 6
-2
votes
2 answers

Aggregating values from the UNION set operator

How should we need to aggregate values, in case, if there are two values in a single column and the rest of the columns contains NULL values, then all the values need to get grouped as well as rest of the column should display NULL as the values We…
Nɪsʜᴀɴᴛʜ ॐ
  • 2,756
  • 4
  • 33
  • 57
1 2 3
16
17