| Author |
Topic  |
|
AskSQLTeam
Ask SQLTeam Question
USA
0 Posts |
|
|
Anonymous
Starting Member
0 Posts |
|
|
graz
Chief SQLTeam Crack Dealer
USA
4128 Posts |
|
|
Anonymous
Starting Member
0 Posts |
Posted - 02/07/2001 : 12:57:28
|
| Use NULLIF instead Following the code in the article, the results I get using COALESCE include an unwanted comma + space at the beginning of the final string:
... COALESCE(@EmployeeList + ', ', '') + CAST(Emp_UniqueID AS varchar(5)) ... gives
, 1, 2, 4
I think NULLIF is what you want:
DECLARE @theList varchar(100)
SELECT @theList = NULLIF(@theList + ', ', ', ') + StatusID FROM StatusAssoc WHERE AccountID = 79
SELECT @theList
================ A, B, C
NULLIF returns NULL if the first value equals the second value. So, the final string is pieced together like this:
Step 1. NULL + Step 2. + ', ' + Step 3. + ', ' +
Unless I'm understanding wrong, it seems you need to use NULLIF instead of COALESCE. Regardless, this is a great tip. |
 |
|
|
Anonymous
Starting Member
0 Posts |
|
|
Anonymous
Starting Member
0 Posts |
|
|
Anonymous
Starting Member
0 Posts |
|
|
mvallani
Starting Member
Canada
1 Posts |
Posted - 03/27/2002 : 19:16:36
|
Anyone have any ideas on the previous post:
Comma delimited list for each sales call Ok, that's great, it gets a comma separated string if you know the sales call id you want. how about getting the string for each sales call in the database e.g.
call id string ------- ------- 1 1, 2, 4 2 2
Thanks
|
 |
|
|
bob s
Starting Member
United Kingdom
7 Posts |
Posted - 08/06/2002 : 23:19:11
|
Hi,
Any ideas how to return all records when they are likely to exceed the datalength applied to the parameter (I am trying to return a string list of all email addresses from a table - am using varchar (8000) as can't use text with '+')
Thanks
|
 |
|
|
markah_1999@yahoo.com
Starting Member
USA
1 Posts |
Posted - 10/09/2006 : 16:00:55
|
using coalesce() in a loop in a tsql sp only works if 1) set the list var = NULL each pass 2) list var is declared varchar
the 1st makes sense. Not sure bout the 2nd... |
 |
|
|
Rukmang
Starting Member
1 Posts |
Posted - 04/06/2007 : 06:49:36
|
Can you please let me know the usefulness of third argument ('') in the coalesce statement. |
 |
|
|
graz
Chief SQLTeam Crack Dealer
USA
4128 Posts |
Posted - 04/06/2007 : 07:51:23
|
COALESCE can accept any number of parameters. It returns the first parameter that isn't null.
=============================================== Creating tomorrow's legacy systems today. One crisis at a time. |
 |
|
|
snSQL
Flowing Fount of Yak Knowledge
USA
1837 Posts |
Posted - 04/06/2007 : 11:17:27
|
quote: Originally posted by Rukmang
Can you please let me know the usefulness of third argument ('') in the coalesce statement.
Did you even look at Books Online (the product documentation)?! http://msdn2.microsoft.com/en-us/library/ms190349.aspx |
 |
|
|
samwise_007
Starting Member
3 Posts |
Posted - 04/09/2007 : 12:48:38
|
>>Can you please let me know the usefulness of third argument ('') >>in the coalesce statement.
There is no third statement, the first comma is in quotes. I was wondering the same thing, since a hardcoded value in the second param would mean coalesce never reaches the third. |
 |
|
|
samwise_007
Starting Member
3 Posts |
Posted - 04/09/2007 : 12:51:56
|
| With coalesce, the second param is required(the value to return if null) - so it says if @variable is null, return 'nothing'. |
 |
|
|
ritush
Starting Member
2 Posts |
Posted - 09/25/2007 : 03:13:12
|
Hi, am using following query to generate a comma-seperated list of the personnumbers. There are about 3,35,000 records in the column for which the csv is generated. The query works fine if the size of varchar is defined as 100 but then the most obvious problem with that is: it doesn't generate the csv for all 3,50,000 records. As a part of workaround, I defined the varchar to have the max size but now the problem is: there is a serious performance hit and the query is taking more than 20mins and is still executing...I'm kind of stuck here as I don't know the workaround for this. This is very critical for my project and any help on this regard will be highly appreciated. following is the query that am using:
DECLARE @PersonNumber varchar(max) SELECT PersonnelNumber = COALESCE(PersonnelNumber + ', ', '') + CAST(PersonnelNumber AS varchar(100)) FROM person select @personnelnumber
|
 |
|
|
Kristen
Test
United Kingdom
22191 Posts |
Posted - 09/25/2007 : 03:21:31
|
| If you have got thousands of values to be concatenated you need to do that at the front end, not in SQL Server, I reckon |
 |
|
|
ritush
Starting Member
2 Posts |
Posted - 09/25/2007 : 03:30:34
|
| I don't have a fron-end in this case... everything is on back-end... any workaround that we can have for the backend itself?? The database is running on SqlServer2005 |
 |
|
|
Kristen
Test
United Kingdom
22191 Posts |
Posted - 09/25/2007 : 05:15:44
|
I doubt there is a workaround.
That sort of string concatenation is doing:
Allocate 4 bytes of memory Store this 4 byte string. Allocate 8 bytes of memory Move original 4 bytes Append this 4 bytes Allocate 12 bytes of memory ... and at row 335,000: Allocate 1,340,000 bytes of memory Move original 1,339,996 bytes Append this 4 bytes
its a huge amount of memory management.
If you could use a pre-allocate string and fixed length for the individual values you might be able to use STUFF() more effectively.
But really this is not something that SQL Server is going to be good at, and it would be better handled "somewhere else"
Kristen |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
Sweden
29138 Posts |
Posted - 09/25/2007 : 06:09:35
|
If "everything is back-end", why do you need to concatenate the personnelnumbers at all?
E 12°55'05.25" N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
India
22460 Posts |
Posted - 09/25/2007 : 08:19:41
|
quote: Originally posted by Peso
If "everything is back-end", why do you need to concatenate the personnelnumbers at all?
E 12°55'05.25" N 56°04'39.16"
OP wants to see the result in Query Analyser 
Madhivanan
Failing to plan is Planning to fail |
 |
|
Topic  |
|