Wednesday, May 13, 2015

How to update rows randomly

update dbo.Table 
set Field = Value 
where Id in ( select top 50 percent id from dbo.Table order by NEWID() )

Handling Multiple FULL OUTER JOIN in sql

SELECT A.column2 , B.column2 , C.column2 FROM ( 
 (SELECT month, column2 FROM table1) 

 FULL OUTER JOIN 
 (SELECT month, column2 FROM table2) 
B on A.month= B.month 
 FULL OUTER JOIN 
 (SELECT month, column2 FROM table3) 
C on ISNULL(A.month, B.month) = C.month )

How to update rows with a random date

UPDATE table SET datetimecol = DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0)

How to convert IList to IQuerable

IQueryable query = new List() { 1, 2, 3, 4, 5}.AsQueryable();

How to check if IQueryable result set is null or empty

list will never be null with LINQ; it will simply represent an "empty collection" if need be. The way to test is with the Any extension method:
if (list.Any()) 
{
// list has at least one item
}

What “exec sp_reset_connection” shown in Sql Profiler means?

sp_reset_connection indicates that connection pool is being reused