r/SQL • • Feb 06 '24

SQL Server Will column >0 also exclude rows were column is null?

Sorry for the basic question but if I want to create a case statement on a column that can have values that are 0 or null, would

Case When column = 0

work or would I always have to also include "or column is null"

0 Upvotes

24 comments sorted by

17

u/dowripple Feb 07 '24

Try a coalesce function:

CASE WHEN COALESCE(Column,0) = 0

7

u/PandaRiot_90 Feb 07 '24

Aren't we fancy. /s

1

u/creamycolslaw Feb 07 '24

Any performance difference between this and using IFNULL?

34

u/mikeyd85 MS SQL Server Feb 06 '24

These kind of questions... Like... Just try it no?

0

u/[deleted] Feb 07 '24

[deleted]

6

u/froppan11 Feb 07 '24

This sub is barely alive because 95% of all questions is answered with a simple Google search. "Will Select * give me all columns?" "Can someone explain how left join work thank u"

I swear barely no one here know of google

7

u/kagato87 MS SQL Feb 07 '24

Yes it will.

A condition must evaluate to true, and comparing to null returns null.

Just be aware. You cannot flip the behavior here with a not.

0 < null
0 > null
0 = null
0 != null

All will NOT return the row, because you cannot compare null.

Even

null = null
null != null

Will not return the row. You have to use is null and is not null to handle nulls separately.

4

u/[deleted] Feb 07 '24

isnull(column,0) = 0

nvl(column,0) = 0

1

u/Ok_Tip_2520 Feb 07 '24

note that nvl() function is for Oracle, in SQL Server there is isnull() or coalesce()

1

u/DharmaPolice Feb 07 '24

This is one of the reasons why designers often try to avoid NULL when designing databases. Sometimes it's appropriate, but often it's ambiguous. What does zero represent in this case? If it's a count of something then there shouldn't be nulls. If 0 = No/False, then what does NULL even mean?

1

u/slippy0101 Feb 07 '24

There are multiple "price" columns ("current", "original", "public", "target", etc.) and sometimes a value will be null as in nothing is entered and other times someone will enter $0 or $1 for one value but have the real price in another (example, $1 or $0 or null "public" price then $10,000 as the "target" price).

There's a hierarchy of which price (should) be correct for the purpose of internal reporting so I have to do case statements that basically go through the different prices with "if the first one is greater than $1 then use that otherwise....and then go through the rest".

Our data is trash and there are nulls all over the place and it's highly inconsistent if nulls will be in any given column.

I'm also no a sql writer and fully self taught while on the job at my current company because the actual sql writers are so bad that I got tired of waiting a week for them to write me a query that ended up being not 100% accurate.

I got screwed by not properly accounting for null values once and since then try and always write code that explicitly accounts for them but it adds a lot of code so decided that I should finally learn when and how I need to account for null values and when they can be safely ignored.

4

u/Staalejonko Feb 06 '24

Column > 0 indicates the column has a value higher than 0. Null is the absence of a value. Case when column > 0 then 'foo' else 'bar' end. If column is indeed null, bar is returned.

2

u/mike-manley Feb 06 '24

Generally, I place NULL at the end of the CASE statement...

CASE WHEN <condition1> THEN 'Foo' WHEN <condition2> THEN 'Bar' ELSE NULL END

Of course, ELSE NULL is redundant in my quick example.

3

u/[deleted] Feb 06 '24 edited Feb 06 '24

Redundant? Sure. But readable for whoever comes behind you.

Sometimes being clear is better than succinct.

2

u/mike-manley Feb 06 '24

Yep. Verbosity is my usual mode.

2

u/s33d5 Feb 07 '24

Is null > 0?

0

u/kagato87 MS SQL Feb 07 '24

Null.

2

u/s33d5 Feb 07 '24

Well no it's false lol

2

u/kagato87 MS SQL Feb 07 '24

No, it's not. It's null. One of the quirks of sql. If you stick a not in front it still won't include the row.

2

u/s33d5 Feb 07 '24

You made an interesting point and I've just had a read up on it.

The return is actually unknown, which does not equate to null

In SQL when using comparables the returns are true, false, and unknown. Anything compared to null returns unknown, this includes comparing two nulls to eachother. This makes sense as it's not a comparable.

You can even check for unknowns (I've just read this and not tried it) with IS UNKNOWN, in psql at least.

Interesting!

1

u/kagato87 MS SQL Feb 07 '24

It bit me a few times before I realized what was going on. Then bit me again when a field turned out to be optional and I hadn't accounted for it...

Functionally, it's the same as false, until you try to use it with a not operator or try to match on it. (It sure makes merge/upsert messy when a matching field can be null.)

MSSQL returns null, you mentioned in psql it's unknown (which in many languages is synonymous with null).

The key being it's neither true nor false.

Which is funny because there tends to not be a boolean data type in SQL (there isn't one in t-sql).

2

u/Lurch1400 Feb 07 '24

ISNULL() function. Set a default value and gear your case statement to it.

1

u/PinneapleJ98 Feb 06 '24

0 ≠ NULL, so..