May 1, 2019 at 9:47 pm
In t-sql 2012, I need to string together lots of names where the end result where each name will be separated by a comma.
The final endput result will look like the following:
Diane Smith, Ed Clark, James Ragon, Bonnie Fline, Lynn Oster.
The maximum length of the names that will be strung together will varchar(127). If the total length is greater than 127, then show all names up to the maximum length.
**Basically a part of one name might be cut off and that is acceptable.
The basic t-sql to obtain this information will obtain all the applicable names from the same table called customers and the t-sql looks like the following:
declare @vendor int = 5689
select vendors.vendorid, vendors.customerid, customers.firstname, customers.lastname
from vendors
join customers on customers.vendorid= vendors.vendorid
where vendorid = @vendor
There is this requirement since the output will be used on an ssrs 2012 report.
Thus would you show me the t-sql that will accomplish this goal?
May 1, 2019 at 10:05 pm
You can use the FOR XML method
declare @vendor int = 5689
select vendors.vendorid, vendors.customerid
, CustomerNames = CAST(STUFF(( SELECT ',' + customers.firstname + ' ' + customers.lastname
FROM customers
WHERE customers.vendorid = vendors.vendorid
--ORDER BY ...
FOR XML PATH('')
), 1, 1, '') AS varchar(127))
from vendors
where vendorid = @vendor
group by vendors.vendorid, vendors.customerid
May 2, 2019 at 3:33 pm
Thanks that works!
May 2, 2019 at 4:33 pm
Now that you have an answer, do you know how it works and why it could fail?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 2, 2019 at 5:56 pm
There is this requirement since the output will be used on an ssrs 2012 report. Thus would you show me the t-sql that will accomplish this goal?
Consider returning all of the data in the column for the report - and limiting the display to the first 127 characters instead. For that text box - you would set the text box value to an expression and the Tooltip to the full column.
The users will then be able to hover over the text box to see the full results - but the report will only show the first 127 characters.
Another option is to size the text box to the size you want and disable the ability for the text box to grow (or shrink). The text box will then display what it can...and hover over the text box to get the full value.
And finally - you could take that string value separated by commas and convert the commas to vbCrLf in the Tooltip expression. This would then display the list vertically when the user hovers over the textbox instead of horizontally.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply