I am returning a table row like so:
ISNULL(I.Name, '') + ', '
This returns the data like so:
One, Two, Three,
But i'm not so sure how to remove the last comma, i have looked at the LEFT and CHARINDEX operators but can't quite figure out the syntax.
Can anyone help?
why take off what you don't need to add on?
let me explain more clearly...
every time you add a term to the list, you're adding a comma-space after it, and the thing is, you don't know when the list is finished, so lopping off the trailing comma is a second operation, not to mention tricky if you have to ask for help on it
my idea is, the other end of the list, the front end, you definitely do know when you've reached it, and that's right at the beginning of your concatenation process, right?
okay, so change this --
ISNULL(I.Name, '') + ', '
to this --
ISNULL(I.Name+ ', ' , '')
ah, the beauty of it
p.s. i nominate the above post for :award: Elegant Solution O' Da Month
Thanks for your suggestion
But that didn't seem to work. Ic an see what you mean, but wouldn't that return a comma after every instance of the row that is returned?
see, this is what happens when you think you are pretty good, you invariably screw up when you say anything about it
i was wrong, it should of course be
Sorry i didn't get a chance to get back to that piece of SQL. Had to work on something else.
Ok that also didn't work.
I tried the IFNULL and got a syntax error.
Is there not a way to do it by "LEFT", "CHARINDEX" AND "SUBSTRING"?
Let's say i have 3 values comma separated i would want them to appear like so:
One, two, three
come on, man, you know the drill here...
you say "i got a syntax error"
then i say "my crystal ball is down at the moment, could you show me the error please"
btw, i know what the error is already, the crystal ball was not needed
Sorry i was trying to do a Remote Desktop to my PC at work but couldn't get connection thus could not show you the error!
Anyway, it's Monday again
The syntax error is:
'IFNULL' is not a recognized built-in function name.
by the way i am using SQL Server 2008 R2
if you haven't figured it out, i made yet another mistake, a typo
instead of IFNULL(', '+name,name) it should have been ISNULL(', '+name,name)
I did think it was a typo when i got that message but when i try your suggestion i get this:
, One, Two, Three
So now there is an extra comma at the beginning
what do you initialize the string with?
obviously there is some kind of looping going on here, right?
I have it in a CROSS APPLY like so:
SELECT ISNULL(', '+I.Notes,I.Notes)
FROM Invoice I
WHERE I.SourceID = B.ID AND I.Notes LIKE '%Discount%'
FOR XML PATH('')
) D (DiscountsUsed)
I couldn't do this insude a subquery as it is returning more than one value so i did it outside and then i just refer to it as "DiscountsUsed"..
Am i doing something wrong?
however, i'm not up to speed on CROSS APPLY and XML PATH so i think perhaps my technique isn't going to help you
Ah i see. It's ok i'm going to try looking at a different method, CHARINDEX or SUBSTRING and LEFT. If i work out a solution i will post the answer here
If you always have comma at the end of the string, you could try this:
SUBSTRING (str, 1, len(str)-1)