sql - Writing my result as horizontal as String -
can me how can rewrite query following result.
1 2 3 4 5 u.t a.h e.z r.z s.a
sometimes return more or less 5 results.
my query:
select left(a.vorname, 1) + '.' + left(a.name, 1) name adr_adressen left join adr_gruppenlink gl on gl.adressnradr = a.adressnradr a.z_klasse = 'ba' , gl.gruppeadr != 'kind'
this you, using row_number()
declare @cols nvarchar(max) select @cols = stuff((select ',[' + convert(varchar,row_number() on ( order left(a.[name],1) ) )+ ']' id [adr_adressen] xml path(''), type).value('.', 'varchar(max)'),1,1, '') declare @query nvarchar(max); set @query = n'select ' + @cols + n' ( select row_number() on ( order left(a.name,1) ) id, left(vorname,1) + ''.'' + left(name,1) name adr_adressen left join adr_gruppenlink gl on gl.adressnradr = a.adressnradr a.z_klasse = 'ba' , gl.gruppeadr != 'kind' ) x pivot ( max(name) id in (' + @cols + n') ) p ' exec sp_executesql @query;
Comments
Post a Comment