SQLTeam.com | Weblogs | Forums

How to select each row 500 records(with comma separate values string) from table in sql server 2016


#1

Hi All,

‌ My table have a 10000 records and my table like
I‌d Name
1‌ abcd
1‌ efgh
1 xyz
.‌........
.‌........
u‌p to 10000records

F‌inally ‌how to select each row 500 records(with comma separate values string) from table
M‌y results like

I‌d Name
1‌ abcd,efgh,xyz(500 records)
1‌ qwe,rty,uio(500 records)
1‌ asd,fgh,jkl(500 records)
‌up to end

P‌lease help me on this.

R‌egards
P‌ols‌‌‌


#2

There are a few ways to do this. One would be:

SELECT 
   Stuff( (
          SELECT ',' + Name AS [text()] FROM dbo.MyTable
          WHERE Id = 1 FOR XML PATH('')
          )
        , 1
        , 1
        , ''
        ); -- the Stuff function is to remove the leading comma

#3

This one is comma separate that is ok...but how to select each row 500 records the we will process comma separate string...