List the Remaining Business Days of this Year in SQL Server

I recently required a query for a report which listed the remaining business days for this year, including the current day. I immediately thought of using spt_values count table.

The spt_values System Table inside the master database is an ideal table to use when you need to use  numbers in your query. This system table is used internally by sql server for various operations. For eg: The query shown here generates values from 0 to 2047

SELECT number from master..spt_values
WHERE type='p'

Let’s use this system table to list the remaining business days for this year:

SELECT DATEADD(d,number,GETDATE())
FROM master..spt_values
WHERE Type='p'
AND DATENAME(WEEKDAY, DATEADD(DAY, number, DATEDIFF(DAY, '19000101', GETDATE())))
NOT IN ('Saturday', 'Sunday')
AND YEAR(DATEADD(d,number,GETDATE()))=2009

OUTPUT

image

To Find the Business Days in a Quarter, check this query of mine

Finding the Business Days In a Quarter and Number them in SQL Server 2005/2008

No comments:

Post a Comment