COALESCE Vs ISNULL Vs REPLACE

COALESCE Vs ISNULL Vs REPLACE

SQL Server has many functions which help developers in many ways. Today I am going to discuss only three functions which appear similar but are different!

Let's discuss them one by one!

1. COALESCE:

This came with MS SQL Server 2008. As per Microsoft following is the definition of this function:

COALESCE Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL. For example, SELECT COALESCE(NULL, NULL, 'third_value', 'fourth_value'); returns the third value because the third value is the first value that is not null.

Basically, whenever you have multiple columns in a table or multiple values where you need to find the first NON-NULL value then you use this function. Following is the example:

SELECT COALESCE(NULL, NULL, 'Hello World')

Output: Hello World

NOTE:

  • The data type of a COALESCE expression is the data type of the input argument with the highest data type precedence. If all inputs are the untyped NULL literal, you get an error.
  • One apparent advantage that COALESCE has over ISNULL is that it supports more than two inputs, whereas ISNULL supports only two. Another advantage of COALESCE is that it's a standard function (namely, defined by the ISO/ANSI SQL standards), whereas ISNULL is T-SQL–specific. These differences between the two functions are fairly straightforward.

2.ISNULL:

This function also came with MS SQL Server 2008 and as per MS definition:

ISNULL replaces NULL with the specified replacement value.

Syntax:

ISNULL ( check_expression , replacement_value )

Example:

The following example finds the average of the weight of all products. It substitutes the value 50 for all NULL entries in the Weight column of the Product table.

USE AdventureWorks2012;

GO

SELECT AVG(ISNULL(Weight, 50))

FROM Production.Product;

GO

OUTPUT: 59.79

Furthermore, you can also replace a value if it's NULL. Please have a look at below example:

SELECT ISNULL(Department, 'TBD') From Department

This will replace department name with TBD whenever it's nULL in the Department Table.

3. REPLACE:

This function also came with MS Server 2008. Basically, whenever you need to replace a value in a string then it helps. Following is the formal definition provided by Microsoft:

Replaces all occurrences of a specified string value with another string value.

Syntax:

REPLACE ( string_expression , string_pattern , string_replacement )

Example :

The following example replaces the string cde in abcdefghi with xxx.

SELECT REPLACE('abcdefghicde','cde','xxx');

GO

Output: abxxxfghixxx


P.S. I hope this would be helpful for you. In case you have any feedback or you want to publish another article on MSBI/SQL Server, kindly post your comment.

Ola Oladunjoye

BI & Data Reporting Analyst at Kensington Mortgages

7 年

Go closer

要查看或添加评论,请登录

Ajay Kumar的更多文章

  • Azure Data Lake Storage: The Ultimate Solution for Big Data Storage and Analytics

    Azure Data Lake Storage: The Ultimate Solution for Big Data Storage and Analytics

    As organizations generate more data, the need for scalable and flexible storage solutions becomes more pressing. Azure…

  • Power BI Best Practices

    Power BI Best Practices

    Dear all Power BI Users, In our day to day work we do design reports/share and publish them. No matter what, we always…

  • The valued competencies of Microsoft Power BI tool

    The valued competencies of Microsoft Power BI tool

    Power BI is a cloud-based business analytic solution, which is used to analyze data from a wide range of data sources…

  • Top 11 Best Practices of Power BI

    Top 11 Best Practices of Power BI

    Hello Power BI Users! Today, in this article, I am going to discuss about top 11 best practices in Power BI. I hope you…

    1 条评论
  • Power BI Data Gateway

    Power BI Data Gateway

    Hello Power BI Users, Lately, I came across a questions ..

  • Export Data From Power BI Dataset

    Export Data From Power BI Dataset

    Dear All, Most of times, I came across a question that how to extract any number of rows data from Power BI dataset…

    1 条评论
  • Power BI - Auto Recovery

    Power BI - Auto Recovery

    Many times, it happens that a Power BI user is working on a Power BI report/file and suddenly system crashed out or…

    1 条评论
  • Why Power BI Premium?

    Why Power BI Premium?

    Microsoft introduced Power BI in late 2013 as a separate software and since then it's continuously growing and now it's…

  • #1 Data Visualization & Analytics Tool

    #1 Data Visualization & Analytics Tool

    Good news for all Power BI Developers/Users. Once again, #Gartner named, Power BI as #1 Data Visualization & Analytics…

  • History of Ms Power BI

    History of Ms Power BI

    Everyone of us who all are working on Ms Power BI, must have idea about Power BI and what does it do but only few are…

社区洞察

其他会员也浏览了