Solved

Benefit of STUFF verseus coalesce

Posted on 2013-01-30
3
1,728 Views
Last Modified: 2013-01-31
I saw an article a while back comparing the speed of STUFF versus coalesce.

Can someone give me a quick reason why its actually supposed to be better?

Select *,STUFF(( SELECT ' ' + CC.[First Name] + ' ' + CC.[Last Name] + '-' + B.Notes   + CHAR(10)                    

                  FROM  dbo.[Client Contact Events] AS B

                  INNER JOIN dbo.[Client Contacts] CC

                  ON B.[Contact ID] = CC.[Contact ID]

                  WHERE [CVT].[Client ID] = B.[Client ID]

                        AND   B.EventType = 'Visit'

                        AND   B.[Contact Date] > [CVT].createdDate  FOR XML PATH ('')),1,0,'') AS [Contact Notes]
From mytable

Open in new window

0
Comment
Question by:lrbrister
3 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 250 total points
ID: 38835928
https://msmvps.com/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx

I don't know about speed, but here is an article talking about the creation of a concatenated
string using FOR XML PATH rather than

DECLARE @X varchar(200);
SELECT @X = COALESCE(@X , '') + YourField FROM YourTable;
SELECT @X;



which can go wrong if you introduce for example

DECLARE @X varchar(200);
SELECT @X = COALESCE(@X , '') + YourField FROM YourTable ORDER BY LEN(YourField);
SELECT @X;

-----

however STUFF and COALESCE seem to be asides here, they are only being used to tidy things up.  It is really an argument between the 'SELECT @VARIABLE = ..'  method and 'FOR XML PATH'

personally I think 'FOR XML PATH' has problems
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 38835958
STUFF and COALESCE do two different things, so you can't really make a direct comparison between them.

I use only FOR XML PATH, since there are documented issues with "SELECT @variable =", particularly with parallelism, and MS has explicitly stated that it's not supported.
0
 

Author Closing Comment

by:lrbrister
ID: 38839681
Thanks guys.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Sum totals 2 21
Querying data from 3 SQL tables 2 30
Help Required 3 89
SQL Improvement  ( Speed) 14 26
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now