Don Kiely's Technical Blatherings

All Things Technical in .NET, SQL Server, and Security

<January 2009>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567


Navigation

Personal

Subscriptions

News

Post Categories



COALESCE as Concatenation Operator

I always forget the details of how to do this and have to research it again. So here for both your and my benefit is how you can use the SQL Server COALESCE function to retrieve text from multiple records and concatenate it together.

The need I had today was to pass a string like '7,2,8' to a stored procedure and get back a string like this: ‘Alaska Natives, Art Education, Art History’. You can probably guess that the numbers are primary keys into a lookup table with areas of interest for grant funding. The numbers come from reading the value of a collection of checkboxes on an ASP Web form (yes, ASP classic, sigh).

I could have done three (or however many) calls to the database, but that is bad practice. So I used the COALESCE function like this (this is my test code before I turned it into a stored procedure:

DECLARE @criteria NVARCHAR(1000)
DECLARE @aoilist NVARCHAR(100)
SET @aoilist = '7,2,8' -- test data; string can be any number of indices

SET @aoilist = ',' + @aoilist + ',' -- put delimiters at start and end

SELECT @criteria = COALESCE(@criteria + ', ' + aoi.ncv_DecAreaOfIntr_Desc, aoi.ncv_DecAreaOfIntr_Desc)
FROM tblFunder_DecAreasOfInterest_dfai aoi
WHERE CHARINDEX(',' + CAST(int_Recordid AS nvarchar) + ',', @aoilist) > 0

PRINT @criteria

According to BOL, the COALESCE function “Returns the first nonnull expression among its arguments.” That description doesn’t begin to hint at this trick, though!

Most commonly the WHERE clause uses an EXISTS clause with a subquery to return data from another table. But in this case another table wasn’t involved, and I just used the @aoilist value to provide the test.

I originally learned of this trick  a few years back in the ASP.NET forums, on which I’m a moderator. It has simplified my coding a lot since then!

 

 

posted on Sunday, January 29, 2006 1:00 PM by donkiely


# Quality and repsonsibility of bloggers @ Thursday, February 02, 2006 7:09 PM

Blogs are becoming, if not already, one of the prime sources of information, especially technical information....

Anonymous

# re: COALESCE as Concatenation Operator @ Monday, February 20, 2006 3:25 PM

Thanks for the feedback, Simon. I'm not thrilled to be held up as an example of irresponsible blogging, but your point is well made.

The point of the blog entry you link to is not the where clause, the point is a cool technique of using coalesce. I was so focused on that particular technique that I included test code that has the ugly where clause in it. I'm glad at least that you approve of the use of coalesce.

I wasn't focused on the where clause, since that is irrelevant to the topic of the post. In this case, however, it was a solution to a rather ugly database and Web design that I had very little control over in advance of a complete system design, used by an aging ASP classic Web site.

So yes, the WHERE clause is ugly. So ignore that part, everyone. It solved a nasty problem I had, but is not, in general, good practice. And Simon has reminded me that people are looking at even the irrelevant parts of my posts included only for completeness.

donkiely

# Quality and repsonsibility of bloggers @ Tuesday, May 23, 2006 5:11 AM

Blogs are becoming, if not already, one of the prime sources of information, especially technical information....

Anonymous




Powered by Dot Net Junkies, by Telligent Systems