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.
ALTER function [dbo].[column_elements_casted] ( @projectid int)
declare @res varchar(Max)
select @res = coalesce(@res + ', cast ([' + ELEMENT_NAME + '] as ' + Element_Type_Name + ') as [' + ELEMENT_NAME + ']', '')-- + ELEMENT_NAME
WHERE Project_ID = '53'