Scott on Writing

Musings on technical writing...

Returning the First Day of the Month from a Given Date/Time value in SQL Server

First off, it's been a long time since my last post, close to a month! Things have been very busy between the new batches of Working with Data in ASP.NET 2.0 tutorials, the monthly Toolbox column, other miscellaneous writings (working on an article on JSON for MSDN Online with my favorite coauthor Atif Aziz), and a couple ongoing consulting jobs. And of course the holiday season is right around the corner! Sadly, this blog falls a little low on the list of priorities. But I digress - enough excuse-making for one entry. Let's get to some technical content!

SCENARIO: Given a date/time value in SQL Server, you need to compute the first of the month in T-SQL. Perhaps you have a stored procedure and are passed in some date/time value and need to store the first of the month in a particular record. How do you do this?

SOME OPTIONS: Sadly, T-SQL doesn't have a DateSerial-like built-in function. DateSerial, in VBScript, allowed you to construct a date object by passing in the year, month, and day as three parameters. If T-SQL did have such a function, we could just do:

DATESERIAL(YEAR(dateValue), MONTH(dateValue), 1)

However, T-SQL does allow for date values to be constructed as strings, so you could append the year, a hyphen, then month, a hypen, and the string “01” to end up with a string like “2006-12-01”. So that is one option.

Another one that feels a bit less dirty is to use the DATEADD function to subtract the current date minus 1. For example, if today is December 12, 2006 December 16, 2006, to get the first of the month take the current date and subtract the day (16) minus 1. That is, subtract 15 days. Voila, you're now at December 1, 2006! This can be accomplished with the following T-SQL statement:

DATEADD(dd, -(DAY(dateValue) - 1), dateValue)

Computing the first of the month from a given date/time value is also useful if you need to compute the last day of the previous month. This can be computed by subtracting one day from the first of the month (simply omit the -1 in the above statement).

For more T-SQL date/time-related recipes check out Date and Time Manipulation in SQL Server 2000 by Manuj Bahl.

posted on Tuesday, December 05, 2006 9:19 PM

Feedback

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 12/6/2006 8:58 AM Mike Hamilton

Your example says if we are using December 12 and subtract the day (16) minus 1 to subtract 15...

???

I am guessing you changed the date and missed the rest of the line.

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 12/6/2006 9:36 AM Scott Mitchell

Good catch, Mike. I've fixed this typo in the blog entry. Good to know at least one person out there reads my blog! :-p

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 12/13/2006 9:55 AM Ben Strackany

For sake of completion, a third (dirty) way would be to exploit the fact that datetimes are numeric and 1.0 equals a day. So to get the beginning of the month from July 15, 2006, you could write your DAY() subtraction technique as

declare @mydate datetime
declare @startofmonth datetime
set @mydate = '20060715'
set @startofmonth = @mydate - DAY(@mydate) + 1
print @startofmonth

Obviously it would be best to stick with documented methods instead of the above. But I thought it was interesting.

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 12/13/2006 11:43 AM JoeBeam

--get first day of month
set @first_day = DATEADD(mm, DATEDIFF(mm,0,@month_year), 0)
--get the first day of the next month
set @first_day_next_month = DATEADD(mm, DATEDIFF(mm,0,@month_year) + 1, 0)

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 2/6/2007 11:03 AM Deathtospam

-- Declare the local variables.
DECLARE @dtValue datetime, @dtFirstOfMonth datetime

-- Initialize [@dtValue] with an arbitrary datetime value.
SELECT @dtValue = GETDATE()

-- Initialize [@dtFirstOfMonth] with a datetime value of
-- the first day of the month/year specified by [@dtValue].
SELECT @dtFirstOfMonth = CAST((CAST(YEAR(@dtValue) AS varchar) + '-' + CAST(MONTH(@dtValue) AS varchar) + '-1') AS datetime)

-- Display the information for viewing in Query Analyzer.
SELECT @dtValue AS dtValue, @dtFirstOfMonth AS dtFirstOfMonth

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles