vhaum
asked on
SQL function with coalesce not returning first column
I have the following function that returns a string for pivoting data. This function also casts it based on a value from another field.
When I try this it doesn't return the first value but does return subsequent values. I.E. a project should have 4 columns but only comes back with three.
When I try this it doesn't return the first value but does return subsequent values. I.E. a project should have 4 columns but only comes back with three.
ALTER function [dbo].[column_elements_casted] ( @projectid int)
returns varchar(Max)
as
begin
declare @res varchar(Max)
select @res = coalesce(@res + ', cast ([' + ELEMENT_NAME + '] as ' + Element_Type_Name + ') as [' + ELEMENT_NAME + ']', '')-- + ELEMENT_NAME
from dbo.Project_Element_Names
WHERE Project_ID = '53'
select @res
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I was thinking it was null, I was having a hard time remembering where to set it to ''.
Thanks
Thanks
Open in new window