Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return every nth record

Status
Not open for further replies.

747576

Programmer
Jun 18, 2002
97
GB
Hi,

I am trying to write a query to return every nth record i.e. every 20th

I am using the sql:

Code:
select * from TABLE
where RIGHT(ID,4) % 20 = 0 
and ID like 'TEST%'


where the ID is like 'TEST0001' and so on. I have 100 records for example but if returning every 20th record I'd expect my first record id from the query to be TEST0020, but the first record seems to always be the 30th.

Can someone check the sql, I am using sql2000

Thank You
 
When you say the 30th record, are you saying it is returning TEST0030? I'm not sure why that could be happening. If I try SELECT '0030' % 20, I get a result of 10 (using SQL 2005 however). Maybe you should try casting to INT first.
 
try...

Code:
select * from TABLE
where RIGHT(ID,4) % 20 = 0
and ID like 'TEST%'
[!]Order By ID[/!]


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top