Sunday, 30 September 2018

SQL Server While Loop from 1 to 1000

declare @n as int=1
while @n <1001
begin
print @n;
set @n =@n+1
end;

SQL Server Group Concatenate

select distinct TRY_CAST(a1.group as varchar(max)) as group,
substring((select distinct ', '+try_cast(a2.delimited field
as varchar(max))
from table_name as a2
where try_cast(a2.group as varchar(max)) = try_cast(a1.group as varchar(max))
order by ', '+try_cast(a2.delimited field
as varchar(max))
for xml path('')),2,8000) as AliasFx
from table name a1
group by cube(a1.group)
order by 1,
AliasFx