• RV
    • Missy – My Home
    • JBAM – Newmar Dutch Star
    • Shaneeda
    • Buying a Used RV
    • Gas vs Diesel RVs
    • Ideal Motorhome
    • Choosing the Ideal RV
  • Cycling
  • Jeep
  • About
  • Flying
    • Vans RV-4
    • Daisy – RV-3B
  • Resources
  • Subscribe

JdFinley.com

Fulltime RV Living Adventures

  • Is Solar For You?
  • Gas vs Diesel RVs
  • Buying a Used RV
  • Choosing the Ideal RV
  • Ideal Motorhome

T-SQL Week End Dates

October 15, 2011

Microsoft SQL Server 2000 2005 2008 T-SQL Tips How-To Development Programming

Occasionally, when querying for report data, you need to divide the data into weeks.  This function returns a table that contains the date of the end of each week that ends in the specified month.

The function consumes two other functions that use statements specified in this T-SQL Begin Date post and this T-SQL End Date post.

Returning a table from a routine such as this is usually very helpful. It can easily be used in JOIN’s and/or WHERE clauses.

CREATE FUNCTION [dbo].[ft_tfm_WeekEndDatesForMonth] (
@InputDate DATETIME
)
RETURNS @Results TABLE
( SequenceNumber INTEGER IDENTITY(0,1)
,EndDay    DATETIME
)
AS
BEGIN

DECLARE @MonthBegin DATETIME
DECLARE @MonthEnd DATETIME
DECLARE @tmpDate DATETIME

SET @MonthBegin = dbo.f_tfm_FirstDayOfMonth(@InputDate)
SET @MonthEnd  = dbo.f_tfm_LastDayOfMonth(@InputDate)
SET @tmpDate  = @MonthBegin

WHILE (@tmpDate <= @MonthEnd)
BEGIN
IF DATEPART(dw, @tmpDate) = 3 --(= Tuesday)
BEGIN
INSERT @Results (EndDay)
VALUES (@tmpDate)
END

SET @tmpDate = DATEADD(dd, 1, @tmpDate)
END

RETURN

END
GO

This routine has made a number of reports much easier to write. I hope you find it useful too!

(Visited 127 times, 1 visits today)

Related posts:

Default ThumbnailT-SQL End Dates with DateAdd Default ThumbnailT-SQL Begin Dates Default ThumbnailWhat is SQL? Default ThumbnailSQL Common Table Expressions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Is it ignorance or apathy? Hey, I don’t know and I don’t care.

— Jimmy Buffett

Popular Posts

  • Powermatic Model 90 Wood Lathe & VFD
  • Wood Turning on a Lathe
  • Chainsaw Review: Sportsman 20 in. 52cc
  • Cielo Grande Barbed Wire Fence Repair

Email Notification


 

Posts by Subject

3DPrinting Aircraft Airstrip Bible Bus bus boys Camping Cat Computers Cycling Development Electrical electronics Entertainment Environment family FAQ Finances flying Food God Health Holiday Home Humor kayak Life Maintenance Missy Nature Photography Review RV-3 RV-4 sailboat sailing self improvement Sewing solar Travel Video weather wood woodworking X-Country

Posts by Month

© Copyright © 2025 JDFinley.com · All Rights Reserved · Privacy Policy

Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to JD Finley and JdFinley.com with appropriate and specific direction to the original content.