Questions tagged [nullif]

NULLIF is an ISO/ANSI SQL standard function that takes two arguments. If the two arguments are equal, then the NULL value is returned. Otherwise, the first argument is returned.

The NULLIF function takes two arguments. If the two arguments are equal it returns NULL. Otherwise, the first argument is returned.

Syntax:

NULLIF ( <expression 1>, <expressions 2> )

It is the same as the following CASE expression:

CASE WHEN <expression 1> = <expression 2> THEN NULL
     ELSE <expression 1>
END
FROM <table>;

In action:

SELECT NULLIF(100, 100); -- the same values, NULL 
Result: NULL              

SELECT NULLIF(101, 100); -- different values, returns the first.
Result: 101                   

It is available in MS SQL Server, SQL, Oracle, Teradata, etc.

61 questions
12
votes
5 answers

Is there an equivalent of SQL NULLIF function in c#?

Is there an equivalent of SQL NULLIF function built-in within c#? Example of what it could look like : double? result double denominator = 0; double Numerator = 100; result = Numerator / NullIf(denominator, 0); result = Numerator /…
AXMIM
  • 2,424
  • 1
  • 20
  • 38
4
votes
1 answer

NULLIF/IIF returning undesired results when comparing Khmer to zero length string

I have find a T-SQL statement failing to make a string NULL, if it's empty string. It's like: SELECT NULLIF(N'ធ្វើឱ្យ', '') -- NULL According to google translator the word means "make" on Khmer language. If I try: SELECT IIF(N'ធ្វើឱ្យ' = '', 1, 0)…
gotqn
  • 42,737
  • 46
  • 157
  • 243
4
votes
5 answers

T-SQL NULLIF returns NULL for zero

Why the script below returns NULL instead of 0? DECLARE @number BIGINT = 0; SELECT NULLIF(@number, ''); According to the MSDN, it should return 0: NULLIF Returns a null value if the two specified expressions are equal. For SQL server, 0 and…
DNac
  • 2,663
  • 8
  • 31
  • 54
3
votes
1 answer

PostgreSQL: How to avoid divide by zero?

I am trying to get the average occupation rate from table occu_cap but I am getting the error "ERROR: division by zero". There are 0 values in both columns. I've been looking at using NULLIF(column_name,0) but I can't figure out how to implement…
Ben Watson
  • 119
  • 6
3
votes
1 answer

Returning alternative value if null?

Hello I am using postgres 12. I've been learning for a few days now. I wanted to know if it's possible to write into the CREATE TABLE stage: IF column_x is NULL, return 'alternative value'? This is my current table: CREATE OR REPLACE FUNCTION…
Lebene
  • 35
  • 4
3
votes
1 answer

IFNULL() Equivalent in PostgreSQL

I am working on a project of migrating from MySQL to PostgreSQL, some function can't works well in PostgreSQL like IFNULL function. Some tutorials say that in PostgreSQL we can use NULLIF to handle it. When I try I get an issue "argument of NOT…
Ugy Astro
  • 357
  • 3
  • 6
  • 16
3
votes
3 answers

SQL Server : ISNULL(compound NULL condition, 'a string') returns only the 1st character, under certain circumstance(s)

I'm a self-taught, vaguely competent SQL user. For a view that I'm writing, I'm trying to develop a 'conditional LEFT' string-splitting command (presumably later to be joined by a 'conditional RIGHT' - whereby: If a string (let's call it…
underscore_d
  • 6,309
  • 3
  • 38
  • 64
3
votes
2 answers

Delphi XE2: How to pass a field with null value?

I am converting a application from Delphi 5 to Delphi XE2 and came across a situation using the BDE. In Delphi XE2 he does not accept pass null for a field that is a foreign key that follows is how this currently working in Delphi…
2
votes
2 answers

In SQLite, how do I query the changed values in two tables, and NULL out the matching values

If I have two rows in two different tables such as: Table Name - Old id title views wx How to clean a drill 30 np Craziest Fails 400 zo Eating Challenge 8 lf JavaScript Tutorial 0 Table Name - New id title views wx How to…
waivek
  • 193
  • 1
  • 5
2
votes
1 answer

Dividing in a case statement and answer is always zero

I'm trying to divide in a case statement. I used nullif to control for the divide by zero error. However, my response is always 0. I'm not sure what I'm doing wrong. Here is the statement: sum(CASE WHEN aw.goalmet LIKE 'yes' THEN 1 ELSE 0…
Julie
  • 21
  • 1
  • 2
2
votes
0 answers

NULLIF truncating date values only when in a UNION ALL Statement in MySql

I am using a statement similar to this to union the results to two similar tables. select sale_time, nullif(sale_time, '0000-00-00') as 'nullif_sale_time' from tblCompletedSales union all select sale_time, nullif(sale_time, '0000-00-00') as…
bconrad
  • 402
  • 4
  • 14
2
votes
2 answers

aggregate functions are not allowed in WHERE - when joining PostgreSQL tables

In a game using PostgreSQL 9.3.10 some players have paid for a "VIP status", which is indicated by vip column containing a date from future: # \d pref_users Column | Type | Modifiers …
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
2
votes
2 answers

ssrs nullif fully qualified name

I am building a report in SSRS that divides two numbers to get their percentage. There are some values that will be divided by 0, which throws an error. I have this expression to get around the divide by 0 error, but it has to run the equation…
Spen
  • 43
  • 2
  • 5
1
vote
1 answer

MySQL: Get all combinations

given I have a user table users: user_id name 1 Bob 2 Adam 3 Charlie Now I want to get all combinations of one or two…
Thomas S
  • 49
  • 3
1
vote
1 answer

check for 'null' timestamp values

I wanted to check if a field has a "null" value. If so, I wanted to replace the "null" with a NULL value. This works for normal fields: SELECT NULLIF(JSON_DATA:"TO_Creator"::string, 'null') as "TO_CREATOR" FROM TABLE However, it doesn't work with…
x89
  • 2,798
  • 5
  • 46
  • 110
1
2 3 4 5