[Solved] How to find dates between two dates in SQL Server 2005

This will work in sqlserver 2005: DECLARE @startdate datetime DECLARE @enddate datetime SELECT @startdate=”2015-12-04″, @enddate=”2015-12-07″ ;WITH N(N)AS (SELECT 1 FROM(SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1)M(N)), tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a,N b,N c,N d,N e,N f) SELECT top (datediff(d, @startdate, @enddate) + 1) dateadd(d, N – 1, … Read more

[Solved] Simulate a dead lock on SQL server using single client and single session

This is currently possible. The following code deadlocks itself BEGIN TRAN CREATE TYPE dbo.OptionIDs AS TABLE( OptionID INT PRIMARY KEY ) EXEC (‘DECLARE @OptionIDs dbo.OptionIDs;’) ROLLBACK This is a long standing issue due to the use of internal system transactions when creating the instance of the TVP that can’t access the lock taken by the … Read more

[Solved] Simplest way to fill working day table

You can use a recursive CTE to accomplish this. This only excludes the weekends. Using DATEFIRST you can figure out what day is a weekend. This query should work no matter what day of the week is set to DATEFIRST. ;WITH DatesCTE AS ( SELECT CAST(‘2016-01-01’ AS DATE) AS [workingDays] UNION ALL SELECT DATEADD(DAY, 1, … Read more