Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Get Number Of Business Days Between Two Dates

Posted on July 31, 2013October 26, 2015 By David Kittell
CREATE FUNCTION fnBusinessDays (
	@StartDate DATETIME
	,@EndDate DATETIME
	)
RETURNS INT
AS
BEGIN
	IF (
			@StartDate IS NULL
			OR @EndDate IS NULL
			)
		RETURN (0)

	DECLARE @i INT = 0;

	WHILE (@StartDate <= @EndDate)
	BEGIN
		SET @i = @i + CASE
				WHEN datepart(dw, @StartDate) BETWEEN 2
						AND 6
					THEN 1
				ELSE 0
				END
		SET @StartDate = @StartDate + 1
	END -- while

	RETURN (@i)
END -- function
GO

SELECT dbo.fnBusinessDays('2016-01-01', '2016-12-31')

Source: http://www.sqlusa.com/bestpractices/datetimeconversion/

Originally Posted on July 31, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL MSSQL - Functions SQL

Post navigation

Previous post
Next post

Related Posts

MSSQL – Datetime In Local Timezone

Posted on February 2, 2017March 26, 2021

This function will get the local time based on the time zone difference from UTC IF OBJECT_ID(N’[GetLocalDate]’) IS NOT NULL DROP FUNCTION [GetLocalDate] GO CREATE FUNCTION [dbo].[GetLocalDate] (@TimezoneDiffInHour FLOAT = – 5) RETURNS DATETIME AS BEGIN RETURN DATEADD(Hh, @TimezoneDiffInHour, GETUTCDATE()) END; SELECT dbo.GetLocalDate(DEFAULT); — or SELECT dbo.GetLocalDate(-5); SELECT dbo.GetLocalDate(+1); Originally…

Read More

PHP WordPress – Check If MultiSite From CLI

Posted on November 3, 2015

require(dirname(__DIR__) . ‘/html/wp-config.php’); if(defined(‘WP_ALLOW_MULTISITE’)) { if (constant("WP_ALLOW_MULTISITE") == true) { echo "\nThis site is MultiSite\n"; } else { echo "\nThis site is not MultiSite\n"; } } else { echo "\nThis site is not MultiSite\n"; } All information on this site is shared with the intention to help. Before any source…

Read More

PowerShell – Rename Pictures to Image Taken

Posted on November 20, 2015December 17, 2023

# If you haven’t already done so you will need to run this to be able to execute PowerShell scripts # You will need to run PowerShell as administrator to run this script set-executionpolicy remotesigned <# .SYNOPSIS Renames pictures. .DESCRIPTION The Rename-Pictures cmdlet to rename pictures to a format where…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes