I've written a function to coalesce mutliple rows into one:
This code works fine and brings back all of the names to one column.
However when I uncomment out the order by statement, it only brings back one name.
I have a need to have the list alphabatized so i can compare them. Anyone know why this does not work?
Code:
ALTER FUNCTION [dbo].[CombineRepNames]
(
@CustomerNumber varchar(50)
)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @VAR varchar(1000)
set @VAR = ''
SELECT @VAR = coalesce(@VAR + ' , ','') + name
FROM valignmentmaster
WHERE soldtocustomer = @CustomerNumber
AND productdivision = 'crm'
AND status = 'active'
AND REPCODE NOT LIKE '%910%'
--order by name asc
RETURN RIGHT(@VAR, LEN(@VAR)-3)
This code works fine and brings back all of the names to one column.
However when I uncomment out the order by statement, it only brings back one name.
I have a need to have the list alphabatized so i can compare them. Anyone know why this does not work?