I using SQL Server 2012 and need to use FOR XML PATH to get a comma-separated string for like projects. The query runs and works but when I try to alter #T2, I get an error
String or binary data would be truncated
My code:
select distinct a.WBS1, (select #Team.Team as [text()] from #Team where #team.WBS1 = a.wbs1 for xml path('')) as Team into #T2from #Team a order by WBS1 ASC alter table #T2 alter column Team varchar(236) #Team.Team is defined as varchar(236) after running the code I get #T2.Team is a nvarchar(-1) which I believe is nvarchar(max).
Is there any easy way to convert nvarchar to varchar? Why does #T2.Team end up being a nvarchar if #Team.Team is varchar(236)?
xml.FOR XMLalways returnsnvarchar(max)unless you specify, TYPEin which case you getxml. What's wrong with just usingCONVERT(varchar(max), (SELECT ... FOR XML ...) )Anyway your actual error is clearly because the data is longer than 236 after concatenating.