Home
  Home
Home
Search
Articles
Page Tag-Cloud
  Software
Software Tag-Cloud
Submit Patch
Building from Source
Open Source Definition
  Popular Tags
C Plus Plus
Source Code
Legacy
Class
Console
  Members
Login
Web-Email
Notable Members
  Official
Our Company
Copyright Information
Software EULA
GPL EULA
LGPL Eula
Pre-Release EULA
Privacy Policy
  Support
Make Contact
 
 
Get Days Of Month
This function returns a table which contains one raw per day of the month of a given year and month. This function makes a great addition to a reporting parameter procedure (think SSRS).

Example: SELECT * FROM dbo.DaysOfMonth(2013, 6)
Returns:
1
2013-06-01 00:00:00.000
2
2013-06-02 00:00:00.000
3
2013-06-03 00:00:00.000
...
(all the other days)
28
2013-06-28 00:00:00.000
29
2013-06-29 00:00:00.000
30
2013-06-30 00:00:00.000


CREATE FUNCTION [dbo].[DaysOfMonth]
(
	@Year	INT,
	@Month	INT
)
RETURNS @Days TABLE 
(
	ID INT IDENTITY(1,1),
	Date DateTime
)
AS
BEGIN
	DECLARE @Date	DateTime

	SET @Date = Convert(VarChar(10), @Month) + '/1/' + Convert(VarChar(10), @Year)

	WHILE(MONTH(@Date) = @Month)
	BEGIN--WHILE
		INSERT INTO @Days(Date) SELECT @Date

		SET @Date = DATEADD(D, 1, @Date)
	END--WHILE

	RETURN 
END


Is this code snippet, product or advice warrantied against ill-effect and/or technical malaise? No. No it's not! Not expressed - Not implied - not at all.



Tags:
 Datetime    Function    Snippet    SQL Server  

Created by Josh Patterson on 2/9/2013, last modified by Josh Patterson on 2/10/2013

No comments currently exists for this page. Why don't you add one?
First Previous Next Last 

Login or signup to leave a comment.
 
Copyright © 2017 NetworkDLS.
All rights reserved.
 
Privacy Policy | Our Company | Contact