Sie sind auf Seite 1von 3

Differences Between ISNULL and COALESCE Functions in SQL Server S.

No 1 ISNULL Meaning: This function works like if condition which we use in other program languages. It takes only two arguments, if first argument is Null then it returns second and one more thing is that the second argument should be of same size that means if first argument has varchar(2) and second has varchar(4) then it truncates last characters. Syntax: ISNULL(argument1, argument2) COALESCE Meaning: This function works same as if else statement and it takes multiple arguments and it checks Null values for each if first argument is null then it considers the second value, if the both first and second arguments are nulls then it considers third argument.

Syntax: COALESCE(argument1, argument2, argument3[, argument4, argument6, argument7]) Creating Computed Columns: With COALESCE() we can not create non-persisted computed column, but only persisted column. Number of Parameters: Takes a variable number of parameters.

Creating Computed Columns: With ISNULL() we can create both persisted & non-persisted computed columns. Number of Parameters: Takes only 2 parameters. Note: If we use more than two parameters with the ISNULL function then we must use nested ISNULL functions.

5 6

ANSI standard or not: A proprietary T-SQL function.

ANSI standard or not: ANSI SQL standard.

Returned Data Type: Returned Data Type: Data type returned is the data type Data type returned is the expression of the first parameter. with the highest data type precedence. If all expressions are non-nullable, the result is typed as non-nullable. Whether translating to CASE Whether translating to CASE Expression possible ? Expression possible ? No Can be translated to a CASE expression: For Example, COALESCE (exp_1, exp_2, exp_n)

Can be translated to CASE WHEN exp_1 IS NOT NULL THEN exp_1 WHEN exp_2 IS NOT NULL THEN exp_2 ELSE exp_n END 8 What happens when parameters datatypes are not determined ? If the data types of both parameters are not determined, the data type returned is int. ISNULL(NULL, NULL) Returns int What happens when parameters datatypes are not determined ? At least one of the NULL values must be a typed NULL. If the data types of all parameters are not determined, the COALESCE function will throw an error: COALESCE(NULL, NULL) Throws an error COALESCE(CAST(NULL AS INT), NULL) Returns int 9 Truncating parameter value: The ISNULL() function looks at the first value and the second parameter value is automatically limited to that length . Example: declare @test varchar(3) select isnull(@test, 'ABCD') AS ISNULLResult Output: ISNULLResult ABC Truncating parameter value: COALESCE() does not have this restriction. Example: declare @test varchar(3) select coalesce(@test, 'ABCD') AS coalesceResult Output: coalesceResult ABCD

References: http://msbitechnology.blogspot.in/2012/03/difference-between-isnull-coalesce-and.html http://beyondrelational.com/modules/1/justlearned/388/tips/8865/isnull-vs-coalesce-wrt-tocomputed-columns.aspx http://www.sql-server-helper.com/tips/tip-of-the-day.aspx?tkey=5d72704a-5a72-46c0-9d3ea973493e8360&tkw=differences-between-isnull-and-coalesce-functions http://www.c-sharpcorner.com/UploadFile/rohatash/differences-between-isnull-and-coalescefunctions-in-sql/

And, further updates on difference between questions and answers, please visit my blog @ http://onlydifferencefaqs.blogspot.in/

Das könnte Ihnen auch gefallen