/ Published in: SQL
URL: all frequently used SQL Queries
all frequently used SQL Queries
Expand |
Embed | Plain Text
TO get the date difference BETWEEN two successive rows: SELECT DATEDIFF(second,A.StartDate,B.StartDate) FROM #temp A INNER JOIN #temp B ON A.sno=B.sno-1
Comments
Subscribe to comments
You need to login to post a comment.

Drop all stored procs using Cursor: Alter Procedure dbo.DeleteAllProcedures As declare @procName varchar(500) declare cur cursor for select [name] from sys.objects where type = 'p' open cur
Go Grant Execute On dbo.DeleteAllProcedures To Public Go
loop through all/selected databases using spMSforeachdb system stored procedure or loop through all/selected user tables using spMSforeachtable system stored procedure: http://www.mssqltips.com/tip.asp?tip=1905
To find a column in all the tables of database SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'Pricelistname' )
Check for both NULL and Empty in one statement::
IF ISNULL(@text, '') = ''
Update 0 with ones and 1 with 0 in a single query
Update #tblx SET x= ( case when x=1 then 0 when x=0 then 1
else x end )
Returns all the customer names which doesn't start with alphabet
select customername from customer where customername like '[^A-Z]%' This example finds the rows for authors with last names of Carson, Carsen, Karson, or Karsen. USE pubs GO SELECT aulname, aufname, phone FROM authors WHERE aulname LIKE '[CK]ars[eo]n' ORDER BY aulname ASC, au_fname ASC GO