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


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

No comments: