June 18, 2009

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


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

0 Responses to "List the Remaining Business Days of this Year in SQL Server"
 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions