Home
  Home
Home
Search
Articles
Page Tag-Cloud
  Software
Software Tag-Cloud
Building from Source
Open Source Definition
All Software
  Popular Tags
C Plus Plus
Source Code
Legacy
Class
Networking
  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 First and Last day of Month
Ok, I know this is simple - but the snippet was in my archive so I'm going to post it. These two functions will return the first and/or last day of the month of the passed in datetime.

Example: SELECT dbo.GetFirstDayOfMonth(GETDATE())
Returns: 2013-01-02 00:00:00.000

Example: SELECT dbo.GetLastDayOfMonth(GETDATE())
Returns: 2013-02-28 23:59:59.990

CREATE FUNCTION GetFirstDayOfMonth
(
	@DateTime DateTime
)
RETURNS DateTime
AS
BEGIN
	RETURN Convert(DateTime, '1/' + Convert(VarChar, Month(@DateTime)) + '/' + Convert(VarChar, Year(@DateTime)) +  ' 0:0:0.0')
END
CREATE FUNCTION GetLastDayOfMonth
(
	@DateTime DateTime
)
RETURNS DateTime
AS
BEGIN
	RETURN Convert(DateTime, Convert(VarChar, (DateAdd(MM, 1, @DateTime - Day(@DateTime) + 1) - 1), 101) + ' 23:59:59.99')
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    Formatting    Function    Snippet    SQL Server  

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

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

 
Copyright © 2024 NetworkDLS.
All rights reserved.
 
Privacy Policy | Our Company | Contact