use of coalesce USE [DB_Name] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[sp_FillMarks] @studentId bigint AS BEGIN SET NOCOUNT ON; SELECT ID, Name, Marks FROM t_Results r where STUDENTID = COALESCE(NULLIF(@studentId, '0'), STUDENTID) and Marks >= 80 END Explanation This will select details of student whose id is passed to the... Continue Reading →
Difference between Coalesce and null if
Both are different yet confused most of the times. Coalesce() takes a list of values and returns the first non-null value. If there is no non-null value,it will return the null value whereas nullif() takes two values and returns the first value. If both values are equal, it returns null. See this example to understand in detail.