TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Elmozamil Elamir
When:
18 Jul 2012 2:33 AM
Last revision by
Elmozamil Elamir
When:
18 Jul 2012 6:15 AM
Revisions:
2
Comments:
0
Options
Subscribe to Article (RSS)
Share this
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
Eliminate holidays (weekends, specific day) using T-SQL
Eliminate holidays (weekends, specific day) using T-SQL
Article
History
Eliminate holidays (weekends, specific day) using T-SQL
Eliminate holidays (weekends, specific day) using T-SQL
One of the
tasks
that may we have to deal with is how to
eliminate
weekends and holidays from a period using SQL server, e.g. if you want to calculate number of business days for specific company you may need to eliminate Saturday and Sunday or Friday.
To do this you can use
DATENAME
function to get name of the day and then eliminate this day from your selection.
In the bellow query I calculate the number of business days excluding Friday.
To do this I used Function and then call this function in may select statement.
ALTER
FUNCTION
[dbo]
.
[Caldates]
(
@SDate
Date
,
@EDate
Date
)
RETURNS
int
AS
BEGIN
DECLARE
@WeekEnd
AS
NVARCHAR
(
15
)
DECLARE
@Days
AS
INT
SET
@WeekEnd
=
'Friday' – you can create more than one variable for different days
SET
@Days
=
0
WHILE
@SDate
<=
@EDate
BEGIN
IF
DATENAME
(
dw
,
@SDate
)
<>
@WeekEnd
BEGIN
SET
@Days
=
@Days
+
1
–- you can use this to calculate number of hours spend in business days by multiplying it by the number of hours per day.
END
SET
@SDate
=
DATEADD
(
day
,
1
,
@SDate
)
END
return
@Days
END
Your select statement
SELECT
[dbo]
.
[ Caldates]
(‘2012-01-01’, ‘2012-06-07’)
GO
You can change the above query as you want, and please share another ways to play with date.
Datetime functions
,
Eliminate holidays (weekends
,
specific day) using T-SQL
,
SQL Server 2008
,
T-SQL
[Edit tags]
Leave a Comment
Please add 8 and 6 and type the answer here:
Post
Wiki - Revision Comment List(Revision Comment)
Wikis - Comment List