Monday, September 14, 2009

Select 30% of records randomly in SQL Server

select top 30 percent * from [table_name] ORDER BY NEWID()

select 30% of records of last month

select top 30 percent * from[table_name]
and DatePart(""m"", date_created) = DatePart(""m"", DateAdd(""m"", -1, getdate()))
AND DatePart(""yyyy"", date_created) = DatePart(""yyyy"", DateAdd(""m"", -1, getdate()))
ORDER BY NEWID()

No comments: