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

Postgres SQL : How to replace value in column without adding new column?

I want to replace '' or '0.00' values from specific column with '' only, without adding new column. I was trying to do as below but its not working. DB columns: id rt1 rt2 x 0.345 y 0.00 0.345 Expected result: id rt1 …
John83
  • 97
  • 1
  • 8
1
vote
1 answer

How to sum values from COALESCE with NULL values

I was wondering if it is possible to sum values used in coalesce even though some of them are null. Currently, if any value from coalesce returns null, the result of the sum is null as well. Here is a SQL fiddle with the basic…
John Barton
  • 1,581
  • 4
  • 25
  • 51
1
vote
2 answers

Nested NULLIF (Assign value of a column to null if (condition A) or (condition B)) in SQL without CASE

I have a column C1 with values that can either be 'values' or 'empty' or 'N/A'. | C1 | ------- | | | N/A | |apple| I want to SELECT column C1 in a way that it converts empty values and N/A to NULL using NULLIF. | C1 | ------- |NULL | |NULL…
Arsalan
  • 65
  • 1
  • 8
1
vote
1 answer

how to use NULLIF with aggregate function

I'm using SQL Management Studio 2012. I'm getting a 'Divide by zero error encountered' error. I'm aware this requires the use of NULLIF but I'm unsure how to use it in my query. This is my query select Ward, LocalAuthority, Fiscal, …
Clem_Fandango
  • 254
  • 2
  • 18
1
vote
1 answer

How do I return a null value for a negative number?

I am trying to perform additional aggregated calculations on a field that I created and because some values are negative, it is messing with the averages. If the values were to be 0, it would also mess with the averages so it's important that these…
Dan
  • 51
  • 9
1
vote
0 answers

nullif(0, '') returns null

I have a trigger that should convert empty strings to null for a tinyint(1) boolean column: CREATE TRIGGER convertToNull BEFORE INSERT ON MyTable FOR EACH ROW SET new.myBoolean = nullif(new.myBoolean, ''); The problem is, it also converts 0 to…
seven11
  • 895
  • 1
  • 7
  • 14
1
vote
1 answer

Displaying Row In Resultset Where No Values Exist

I've read a couple of questions on here that seem to answer this e.g. Return a value if no rows are found SQL However, I'm struggling to get it to work for me. This is the output of my query as it stands: As an example there are actually six…
Matt
  • 61
  • 10
1
vote
2 answers

How to faster Mysql Insert with nullif?

i am loading a CSV file through PHP and inserting into a temporary table, this is what my loading PHP script and MySQL query. $i=0; while (($data = fgetcsv($source, 1000, ",")) !== FALSE) { if($i!=0) { …
davidb
  • 263
  • 5
  • 10
  • 23
1
vote
0 answers

How to Find Average of Column Which Has Zeroes?Average seems to ignore ZERO and NULL while calculating

I want to find average as avg = sum(a*b) / ISNULL( NULLIF (sum(b) , 0) ,1) where a or b can be 0
user6754
  • 71
  • 1
  • 8
1
vote
3 answers

Case with NULL expression in SQL Server

I have a problem with this piece of code and i'm pretty sure the solution is quite easy, but i don't see it. CASE @L0 WHEN '/' THEN NULL ELSE @L0 END It is part of code i use to import data out of a flat file (csv), where NULL is stored as /. The…
Tom
  • 17
  • 4
1
vote
1 answer

Are arithmetic equations implicitly evaluated in NULLIF()

My SQL Fundamentals 1 Exam Guide states, select NULLIF(1234, 123+1) from dual; The arithmetic equation is not implicitly evaluated... However, when I submit the query below the result is null - it seems that 123+1 is evaluated (although I know…
Jeff Levine
  • 2,083
  • 9
  • 30
  • 38
0
votes
1 answer

Division by zero exception issue

I am running this query where I calculate : the number of active users in the previous period the number of returning users (users active in the previous period and in the current period) retention rate (…
Steven
  • 19
  • 3
0
votes
1 answer

Show COUNT=0 when grouping by from two tables

I am trying to bin my data to see how it is distributed. Some bins contain no data. So, when I query my bins, I only get the bins containing data. I would like the query to also show empty bins. I was trying to create a table containing all the bins…
0
votes
2 answers

01476. 00000 - "divisor is equal to zero" Oracle

I have the following code as part of a Script: ROUND ( ( (COUNT(DISTINCT CASE WHEN ONLINE_SALES > 0 THEN CONTACT_KEY ELSE NULL END)) / (COUNT(DISTINCT CASE WHEN ONLINE_SALES > 0 OR OFFLINE_SALES > 0 THEN CONTACT_KEY ELSE NULL END)) …
Kaz111
  • 9
  • 4
0
votes
0 answers

operational error pymssql in python. divided zero

I have this query in Sqlserver that works fine: select YEARWEEK_DATE as PERIODO ,banner as BANNER,ubicacion as UBICACION,branchid as BRANCHID ,marca as MARCA, clase as CLASE,sum(unidades_vendidas) as UNIDADES_VENDIDAS, sum(unidades_vendidas *…