Pivoting using Coalesce

Mikhail Firsov 1,876 Reputation points
2021-07-27T10:44:20.423+00:00

Hello!

Yesterday I got the following tip from mssqltips.com: The Many Uses of Coalesce in SQL Server and was puzzled a lot: I still don't understand what "feature" of Coalesce allows it to pivot data.

According to the MS's documentation coalesce just returns the first non-null value and that is all, but when used with the variable - coalesce (variable, '') + ... it may be used to pivot data:
118264-01.png

Here are the two examples showing that adding "coalesce" to the code changes nothing in the output...
118207-03.png
118208-02.png

...so why "coalesce (variable).. makes that variable to contain not only the last value (Executive) as in the second example but all of them instead???

SELECT @DepartmentName = COALESCE(@DepartmentName,'') + Name + ';'
FROM HumanResources.Department
WHERE (GroupName = 'Executive General and Administration')
SELECT @DepartmentName AS DepartmentNames
go

118292-05.png

Thank you in advance,
Michael

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,637 questions
{count} votes

9 answers

Sort by: Most helpful
  1. Viorel 117K Reputation points
    2021-07-27T10:59:23.327+00:00

    Did you also try this?

    DECLARE @DepartmentName VARCHAR(1000) = ''
    SELECT @DepartmentName = @DepartmentName + Name + ';'
    FROM . . .
    

    The queries seem to perform concatenation, which is called "pivot" by the author. Maybe you can ask a question in the comment section.


  2. Mikhail Firsov 1,876 Reputation points
    2021-07-27T11:09:07.35+00:00

    Oh, I did try

    DECLARE @DepartmentName VARCHAR(1000) = ''
    SELECT @DepartmentName = @DepartmentName + Name + ';'
    FROM . . .

    ...but got "null", so this is important: = '' ! Coalesce just adds the '' instead of being typed in the DECLARE ... string!

    Thank you very much!!!

    Regards,
    Michael

    0 comments No comments

  3. Erland Sommarskog 111.1K Reputation points
    2021-07-27T11:55:51.333+00:00

    Beware that the correct result of

    SELECT @DepartmentName = COALESCE(@DepartmentName,'') + Name + ';'
    FROM HumanResources.Department
    WHERE (GroupName = 'Executive General and Administration')
    

    is undefined. That is, you may get what you expect, or you may get something else.

    To compose a string like this, use string_agg if you are on SQL 2017 or higher. For earlier version, use FOR XML PATH. While the syntax of the latter is not particularly intuitive, it is guaranteed to work.

    0 comments No comments

  4. Mikhail Firsov 1,876 Reputation points
    2021-07-27T12:12:43.153+00:00

    ErlandSommarskog, thank you for the clarification!

    ...but why the result is undefined? Doesn't string_agg do the same string concatenation as "+" ?


  5. Erland Sommarskog 111.1K Reputation points
    2021-07-27T12:16:11.49+00:00

    String_agg does it in a defined way. It is an aggregate function.

    SELECT with variable assignment is only well defined when the SELECT statement returns a single row. The Docs says that if you say:

    SELECT @x = col FROM tbl
    

    @x will be assigned the value from the last row, but "last row" has no meaning in a relational database.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.