I have a query running against a SQL Server database. One of the columns in it uses the STUFF()
function.
When I run the query in a SQL editor window, the resulting value looks like this:
1234
When I try to use this query in an SSRS report, SSRS automatically puts a column alias within the STUFF()
function, resulting in a value that looks like this:
<Expr1>, 1</Expr1><Expr1>, 2</Expr1><Expr1>, 3</Expr1><Expr1>, 4</Expr1>
The only difference between the two is that when I save the query in SSRS, the term "AS Expr1" is added automatically (see code below).
How can I prevent SSRS from adding this alias automatically and run the query as it is?
SELECT
FirstName,
LastName,
CourseTitle,
LastLoginDate,
NoOfModules,
COUNT(CourseCompleted) AS ModulesStarted,
STUFF(
(
SELECT ',' + CAST(CourseModule AS varchar(20)) -- SSRS puts "AS Expr1" here
FROM EDSF
WHERE
FirstName = e.FirstName AND
LastName = e.LastName AND
Coursecompleted = '1' AND
CourseTitle = e.CourseTitle
FOR XML PATH('')
),1,1,''
) AS CoursesCompleted
FROM EDSF e
WHERE
Coursecompleted = '1' OR
Coursecompleted = '0'
GROUP BY
FirstName,
LastName,
CourseTitle,
LastLoginDate,
NoOfModules;
EDIT: the code has been rearranged and the question reworded to make the issue clearer. I'm NOT having a problem aliasing the returned column itself: instead, SSRS is actually adding an alias to the subquery within my STUFF()
function, resulting in extra junk within the field itself.