Posted in C# 4.0, SQL Server

SQL Server Tips – Find # of business days between two dates

To find the business days or say working days between two dates. This could be the easiest task for many but I would still like to make a not of this, so I am writing this post.

DECLARE @IsSaturWorking bit
SET @StartDate ='2011/07/01'
SET @EndDate ='2011/07/31'

SELECT  (DATEDIFF(dd, @StartDate, @EndDate)+ 1)  -
 (DATEDIFF(wk, @StartDate, @EndDate)* 2)  -
        (CASEWHENDATENAME(dw, @StartDate)='Sunday'THEN 1 ELSE 0 END)-
        (CASEWHENDATENAME(dw, @EndDate)='Saturday'THEN 1 ELSE 0 END)

I hope this help someone.

Posted in SQL Server

SQL Server Tips – Dynamic Order By

Many times we need to sort records based on some conditions and for that we need to implement conditional sorting in SQL server. I have mentioned some techniques to achieve this below.

Continue reading “SQL Server Tips – Dynamic Order By”