Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get all the dates in a month

Author  Topic 

09999
Starting Member

5 Posts

Posted - 2011-08-02 : 17:18:32
Can any one please tell me how do I write a select query that gives me all the dates present in a specified month. Is there any table in sql server 2008(as I am currently using this one) where I can query?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-02 : 17:42:45
You can do it using a numbers table (or by constructing one on the fly as shown below):
DECLARE @startDate DATETIME;
SET @startDate = '20110801';

;WITH N(n) AS
(SELECT 0 UNION ALL SELECT n+1 FROM N WHERE n < 30)
SELECT
DATEADD(dd,n,@startDate)
FROM
N
WHERE
MONTH(DATEADD(dd,n,@startDate)) = MONTH(@startDate);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-03 : 00:57:48
this is a calendar table function which gives you all dates between a range

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -