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

Nested NULLIF with ISDATE to eliminate records

I have a column (datatype nvarchar(max), not my choice, legacy) with various different uses to the end user dependent on other factors. I was trying to narrow down to certain specific data within that column given earlier clauses within some sample…
cockbeard
  • 11
  • 3
0
votes
0 answers

regexp_substr coalesce nulliff

I am trying to achieve extract the version number of an app. Some rows are missing the app version and i wanted to populate that as well. post_mobileappid app_version My someword 13.0.0 13.0.0 MySomeWord 12.0 12.0 I have the…
0
votes
3 answers

Using Nullif as part of a condition

I'm currently in a scenario where we have a clunky, old, monolith of a database powering one or two of our less user-friendly systems (this is controlled by a third-party, so I can't change that). Going forward, I'm looking to push the necessary…
Andrew Corrigan
  • 1,017
  • 6
  • 23
0
votes
1 answer

Using variable in expression with Nullif fails

I'm using the code below to convert an empty string to Null. I don't get an error but it remains an empty string. I suspect an incorrect use of the variable col_name in expr for col_name in ['col1', 'col2']: df_new = df \ …
John Doe
  • 9,843
  • 13
  • 42
  • 73
0
votes
1 answer

how to use nullif() in a query in a php form

I try to import a csv file in a table in PostgreSQL 13 and php form (beginner). There are two foreign keys integer type in the table, one can be empty. So when I import the file I have the common message invalid input syntax for type integer for one…
Leehan
  • 145
  • 7
0
votes
0 answers

How to find the timestamp of last occurrence of null/special character in mysql?

I currently run this operation regularly to preprocess empty strings and '-1' to null on several columns. Is there any way to find out the last row that has gone through nullif operation or has '' or -1 so I can process everything from that point.…
user12921571
0
votes
2 answers

How to find last row that ran NULLIF() or has null in mysql?

I currently run this operation to convert empty strings to null. Is any way to find out the last row that has gone through NULLIF() operation or has null character so I can process everything from that point. My table has a timestamp column. I have…
user12921571
0
votes
1 answer

NULLIF of FINAL SUM Doubled when using MATCH_RECOGNIZE

When I run the following code, I would expect b1 and b2 to be equal, however, b2 is doubled. Am I doing something wrong? Is this a bug in the database? We're running Oracle 12c (12.2.0.1.0). WITH TBL AS ( SELECT 1 a, 1 b FROM DUAL UNION ALL …
MrMucox
  • 3
  • 4
0
votes
2 answers

NULLIF how to set it properly in this query

I am using the following query to get some tablespaces usage at a glance: db2 "select substr(tbsp_name,1,30) as Tablespace_Name, tbsp_type as Type, substr(tbsp_state,1,20) as Status, (tbsp_total_size_kb / 1024 ) as Size_Meg,…
Mike
  • 1
  • 2
0
votes
2 answers

mysql select count for multiple conditions

I have a table that contains elements. Each element has its parent instance_id, a number_of_lines of code, and a deletion_date when removed. I built a query that counts the elements grouped by instance with totals for elements that have code (…
PA.
  • 28,486
  • 9
  • 71
  • 95
0
votes
6 answers

How can you use NULLIF in the where clause?

I am trying to do something like this: select col_1, col_2, etc from table where col_1 = nullif('', '') Am I doing this incorrectly? I am not getting any results back. Edit: My expected results are to get every record back where…
0
votes
2 answers

Nullif statement in sas

I have a following table Id Values 1 A 1 A 1 B 2 @ 2 @ 2 @ 3 A 3 A 3 A 3 A 3 @ 4 B 4 B 4 B Output: Id Values 1 @ 2 @ 3 …
0
votes
2 answers

Multiply in SQL just if result is not NULL

I have the following query: SELECT (COUNT(DISTINCT CASE WHEN houses.level = 'top' THEN users.id END)::float / NULLIF(COUNT(DISTINCT users.id)::float, 0)) FROM users LEFT JOIN houses ON houses.user_id = users.id And instead of a ratio, I want to…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
0
votes
2 answers

Handle error due to division by zero using SQL

The database engine is a finance software called Paprika. It's purpose is to pull out revenue in December 2018 for a particular department. (December 2018 project value across 3 databases (UK, US, BR)job prob/FX rate)(department hours based on…
0
votes
1 answer

How to deal with divide by zero error in sql

I'm new to SQL and would like to know the best way of dealing with dividing by zeros. I know about the NULLIF function however I'm having a bit of trouble implementing it. In this example, every field could be a zero. What is the best way of going…
Greg
  • 476
  • 9
  • 23