Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
LOG() Function in SQL Server
Next article icon

NULLIF() Function in SQL Server

Last Updated : 25 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

NULLIF() function in SQL Server is used to check if the two specified expressions are equal or not. If two arguments passed to a function are equal, the NULLIF() function returns Null to us. NULLIF() function operates like a Like a CASE Statement. It will return the value of the first argument if the two arguments are not identical.

The ISNULL() function substitutes a placed value for the Null value. The ISNULL () function is frequently used in a variety of circumstances, including switching the Null value in joins and selecting statements from one value to another.

Syntax

NULLIF(expression1, expression2)

Parameters:

NULLIF() Function accepts two parameters,

  • expression1: This is the first expression to be compared.
  • expression2: This is the second expression to be compared with the first.

Return Value:

  • If expression1 equals expression2, the function returns NULL.
  • If expression1 does not equal expression2, the function returns expression1.

NULLIF() Function Examples

Let us take a look at some of the examples of NULLIF() Function in SQL Server.

Example 1: Comparing Two Integers Using the NULLIF() function

Here, we have two integers 11 and we are comparing if they are equal or not if both are equal then it will return null or else the first expression.

Query:

SELECT NULLIF(11, 11);

Output:

NULLIF(11,11)

null

Here, NULL is returned as both expressions are equal.

Example 2: Comparing Two Strings Using the NULLIF() function

Here we have basically two strings and we are checking if they are equal or not as it is equal it will return the first expression.

Query:

SELECT NULLIF('ab', 'abc');

Output:

NULLIF(‘ab’, ‘abc’)

ab

Here, ‘ab’ is returned as output as the stated expressions are not equal.

Example 3: Using NULLIF() function and getting the output using variables.

In this example, we use the NULLIF() function with variables.

Query:

DECLARE @exp1 VARCHAR(50); DECLARE @exp2 VARCHAR(50); SET @exp1 = '2021/01/08'; SET @exp2 = '2021/01/08'; SELECT NULLIF(@exp1, @exp2);

Output:

null

Example 4: Using NULLIF() Function with CAST() Function

Here we are using NULLIF() function and getting the output using CAST() function.

Query:

SELECT NULLIF(11, CAST(11.65 AS int));

Output:

NULLIF(11, CAST(11.65 AS int))

null

Important Points About SQL Server NULLIF() Function

  • The NULLIF() function compares two expressions and returns NULL if they are equal; otherwise, it returns the first expression.
  • It is different from the ISNULL operator, which checks if a value is NULL, and from the ISNULL() function, which replaces NULL values with a specified value.
  • The NULLIF() function simplifies SQL queries by reducing the need for complex conditional logic using CASE statements.
  • It is commonly used to prevent errors such as division by zero or to handle specific conditional logic within queries.


Next Article
LOG() Function in SQL Server
author
nidhi1352singh
Improve
Article Tags :
  • SQL
  • DBMS-SQL
  • SQL-Server

Similar Reads

  • SQL Server ISNULL() Function
    The ISNULL() function in SQL Server is a powerful tool for handling NULL values in our database queries. It allows us to replace NULL values with a specified replacement value, ensuring that your queries return meaningful results even when data is missing. In this article, We will learn about the SQ
    4 min read
  • LTRIM() Function in SQL Server
    The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string, SyntaxThe LTRIM function for SQL Server syntax is: LTRIM(string, [trim_string]) Parameter: string - The string from which the leading space char
    2 min read
  • LOG() Function in SQL Server
    The LOG() function returns the logarithm of a specified number or the logarithm of the number to the specified base. Syntax : LOG(number, base) Parameter : LOG() function accepts two-parameters as mentioned above and described below. number - This parameter hold a number which is greater than 0. bas
    1 min read
  • MIN() Function in SQL Server
    MIN() : This function in SQL Server is used to find the value that is minimum in the group of values stated. Features : This function is used to find the minimum value.This function comes under Numeric Functions.This function accepts only one parameter namely expression. Syntax : MIN(expression) Par
    2 min read
  • RANK() Function in SQL Server
    The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
    5 min read
  • ISNUMERIC() Function in SQL Server
    The ISNUMERIC() function in SQL Server is a critical tool for determining whether a given expression or value can be interpreted as a numeric type. This function is valuable for data validation and ensuring that values conform to numeric formats before performing calculations or other operations tha
    3 min read
  • MONTH() Function in SQL Server
    MONTH() function : This function in SQL Server is used to return the month of the year i.e, from 1 to 12 for a date stated. Features : This function is used to find the month of the year for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, dat
    2 min read
  • LOG10() Function in SQL Server
    This function returns the logarithm of a number to base 10. Syntax : LOG10(number) Parameter : This method accepts a single-parameter as mentioned above and described below. number - This parameter hold a number which is greater than 0. Returns - It returns the base-10 logarithm of the specified num
    1 min read
  • IIF() Function in SQL Server
    IIF() function judges or evaluates the first parameter and returns the second parameter if the first parameter is true, otherwise, it returns the third parameter. IIF() function used in SQL Server to add if-else logic to queries.IIF is not supported in dedicated SQL pools in Azure Synapse Analytics.
    2 min read
  • NCHAR() Function in SQL Server
    NCHAR() function : This function in SQL Server is used to return the Unicode character that is based on the number code. For example, if the specified number is 65 then this function will return A. Features : This function is used to find the Unicode character of a given number. This function accept
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences