Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
heyy..

I want a query that shows days in current month...


ie.. if june is the current month then I want like this....

01
                  02
                  03
                  04
                  05
                  .
                  .
                  .
                  30


if February means ....

01
                   02
                   03
                   04
                   05
                   .
                   .
                   .
                   28


Is there is any way to getting like this....
Posted
Comments
Sudhakar Shinde 26-Jun-13 5:36am    
Following query will give you first and last date of the month.

SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01') AS FirstDayOfNextMonth,LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) AS LastDayOfNextMonth;

You can loop to find out in between dates for display purpose. Instead of Currdate you can pass any date to find out first and last date of the month.

1 solution

Try the following Query


DECLARE @ADate DATETIME
SET @ADate = GETDATE()
DECLARE @Count INT
SELECT @Count=DAY(@ADate)

--Get No of days in Month
DECLARE @rtDate INT
SET @rtDate = CASE WHEN MONTH(@ADate)
IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@ADate) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(@ADate) % 4 = 0
AND
YEAR(@ADate) % 100 != 0)
OR
(YEAR(@ADate) % 400 = 0)
THEN 29
ELSE 28 END
END


DECLARE @intFlag INT = 0
WHILE (@intFlag <= @rtDate-1)
BEGIN
	SET @intFlag = @intFlag + 1
	PRINT @intFlag
END
 
Share this answer
 
Comments
Maciej Los 26-Jun-13 6:21am    
It looks correct but does not returns the collection of dates ;)
A 4!
Vipin kumar.P 26-Jun-13 7:04am    
Can can give me a example so that i can look in to it

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900