Click here to Skip to main content
6,822,123 members and growing! (20,187 online)
Email Password   helpLost your password?
Database » Database » SQL Server     Beginner License: The Code Project Open License (CPOL)

Get a table with all dates in a month

By TDunk

This SQL Server 2005 Table values function returns a table with all dates of a month for a provided date.
SQL, Windows, SQL-Server (SQL2005)
Posted:7 Mar 2008
Views:7,230
Bookmarked:11 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 0.64 Rating: 1.33 out of 5
2 votes, 66.7%
1
1 vote, 33.3%
2

3

4

5

Introduction

When doing different joins it's often usefull to have a table that contains all dates in a specific month.

This is af table-valued function for SQL Server 2005 that creates such a table.

Using the code

The function "getFullmonth" looks like this:

-- =============================================
-- Description: Returns a table containing all dates in the month of the specified date
-- =============================================
ALTER FUNCTION [dbo].[getFullmonth]
(
 @date1 datetime
)
RETURNS @dates TABLE 
(
 date datetime not null 
)
AS
BEGIN
 -- Set first day in month
 DECLARE @month int;
 
 SET @month = datepart(MM, @date1);
 SET @date1 = convert(datetime, convert(varchar,datepart(yy,@date1)) + '.' + convert(varchar,@month) + '.01 00:00:00');
 WHILE datepart(MM,@date1) = @month
 BEGIN
  INSERT INTO @dates VALUES (@date1);
  SET @date1 = dateadd(dd, 1, @date1);
 END 
 RETURN;
END                      

After creating the function it can be used like this for different queries:

SELECT a.date, b.holidayname 
FROM dbo.getFullmonth(getdate()) a 
LEFT OUTER JOIN holiday b on a.date = b.holidaydate
ORDER BY 1, 2

If the above table "holiday" holds all public holidays the result will show the full month with the name for the holidays in the current month.

Points of Interest

The same princip can be used for returning all day numbers in a week.

Happy coding!

History

Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TDunk


Member

Occupation: Chief Technology Officer
Company: TimeMap ApS
Location: Denmark Denmark

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralCan also use CTE in SQL 2005 PinmemberGhepper14:31 2 Apr '08  
GeneralIt is possible to do this without using a table variable PinmemberNoam Helfman11:07 10 Mar '08  
GeneralWeak PinsupporterMark Nischalke5:46 7 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 7 Mar 2008
Editor:
Copyright 2008 by TDunk
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project