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

Monday Date Calculation

Posted on July 31, 2013October 26, 2015 By David Kittell
-- SQL user-defined function
-- SQL scalar function – UDF
CREATE FUNCTION fnMondayDate (
	@Year INT,
	@Month INT,
	@MondayOrdinal INT
	)
RETURNS DATETIME
AS
BEGIN
	DECLARE @FirstDayOfMonth CHAR(10),
		@SeedDate CHAR(10)

	SET @FirstDayOfMonth = convert(VARCHAR, @Year) + '-' + convert(VARCHAR, @Month) + '-01'
	SET @SeedDate = '1900-01-01'

	RETURN DATEADD(DD, DATEDIFF(DD, @SeedDate, DATEADD(DD, (@MondayOrdinal * 7) - 1, @FirstDayOfMonth)) / 7 * 7, @SeedDate)
END
GO

-- Test Datetime UDF
-- Third Monday in Feb, 2015
SELECT dbo.fnMondayDate(2016, 2, 3)

-- 2015-02-16 00:00:00.000
-- First Monday of current month
SELECT dbo.fnMondayDate(Year(getdate()), Month(getdate()), 1)
-- 2009-02-02 00:00:00.000

Source: http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

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

Delimiter Split

Posted on September 30, 2013October 26, 2015

IF OBJECT_ID(N’dbo.fnSplit’) IS NOT NULL DROP FUNCTION dbo.fnSplit GO CREATE FUNCTION dbo.fnSplit( @sInputList VARCHAR(8000) — List of delimited items , @sDelimiter VARCHAR(8000) = ‘,’ — delimiter that separates items ) RETURNS @List TABLE (item VARCHAR(8000)) BEGIN DECLARE @sItem VARCHAR(8000) WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0 BEGIN SELECT @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))), @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList)))) IF LEN(@sItem) >…

Read More

Mac OS X – Get Network Information

Posted on February 27, 2017May 2, 2017

Every once in a while you need to get some basic network information from your Mac or a Mac you are supporting, this script below will help get you some helpful information. #!/bin/sh clear sExternalMACALService="http://dns.kittell.net/macaltext.php?address=" # List all Network ports NetworkPorts=$(ifconfig -uv | grep ‘^[a-z0-9]’ | awk -F : ‘{print…

Read More

Bash – Running In Docker?

Posted on October 26, 2020August 17, 2024

docker=$(cat /proc/self/cgroup | awk -F/ ‘$2 == "docker"’) if [ ! -z "$docker" ]; then echo "Running in Docker" else echo "Not running in Docker" fi Originally Posted on October 26, 2020Last Updated on August 17, 2024 All information on this site is shared with the intention to help. Before…

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