DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to find Monday's date for any date?

The following Microsoft SQL Server T-SQL scalar-valued function returns Monday's date for any input date:

-- SQL Server user-defined function - UDF - SVF

USE AdventureWorks2008;

GO

 

CREATE FUNCTION fnMondayOfWeek

               (@AnyDate DATETIME)

RETURNS DATETIME

AS

  BEGIN

    RETURN dateadd(yyyy,datepart(yyyy,dateadd(weekday,1 -

           datepart(weekday,@AnyDate),@AnyDate)) - 1900,

                   0) + dateadd(dy,datepart(dy,dateadd(weekday,1 -

                   datepart(weekday,@AnyDate),@AnyDate)), 0)

  END

GO

 

SELECT dbo.fnMondayOfWeek('2016-10-23T00:00:00.000')

GO

-- 2016-10-24 00:00:00.000

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE