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

# re: Returning the First Day of the Month from a Given Date/Time value in SQL Server 6/5/2008 5:18 AM Valdemar Felipe Gomes Carneiro

This site have a great solution for select all format of dates from sql 2000.

http://www.sqlservercentral.com/articles/Advanced+Querying/howmanymoremondaysuntiliretire/2475/

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

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles