• 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 Begin Dates

October 1, 2011

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

When programming stored procedures, functions, or just writing queries for Microsoft SQL Server databases, we often need date variables set to the beginning or end of a day, week, month or year. This is commonly done by concatenating strings – such as:

DECLARE @InputDate DATETIME
SET @InputDate = '4/1/2004 ' + '00:00:00'

That works but is pretty lame. This approach becomes downright unfriendly when you need the first day of a particular week or some such.  Follow along and I’ll reveal the easy, T-SQL way over the coming couple of weeks.

Let’s start with the easy scenarios; the beginning of a day, month or year.SQL Server 2000 2005 2008 T-SQL

Function that returns a date set to the first day of the month and first second of the day using the input date as the starting point. When 5/15/2004 is input, the output will be 5/1/2004 00:00:00.

DATEADD(mm, DATEDIFF(mm, 0, @InputDate), 0)

Function that returns a date set to the first day of the year and first second of the day using the input date as the starting point. When 5/15/2004 is input, the output will be 1/1/2004 00:00:00.

DATEADD(yy, DATEDIFF(yy, 0, @InputDate), 0)

Function that returns a date set to the first second of the day using the input date as the starting point.  When 5/15/2004 is input, the output will be 5/15/2004 00:00:00.

DATEADD(dd, DATEDIFF(dd, 0, @InputDate), 0)

 

(Visited 186 times, 1 visits today)

Related posts:

Default ThumbnailWhat is SQL? Default ThumbnailSQL Common Table Expressions Default ThumbnailCompare Date Values Default ThumbnailInterface Programming

Leave a Reply Cancel reply

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

Life is a lemon and I want my money back.

— Meatloaf

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 Factory Five family FAQ Finances flying Food God Health Holiday Home Humor kayak Life Maintenance 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.