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
 Script Library
 fx_LastDayOfMonth

Author  Topic 

Spyder
SQLTeam Author

75 Posts

Posted - 2002-02-03 : 01:35:35
/****** Function dbo.fx_LastDayOfMonth v1.1 ******/

CREATE FUNCTION fx_LastDayOfMonth (@date DATETIME)
RETURNS DATETIME
AS
BEGIN
--ensure valid date
IF ISDATE(@date) = 1
BEGIN
--determine first day of month
SET @date = DATEADD(DAY,-DAY(@date)+1,@date)
--determine last day of month
SET @date = CONVERT(DATETIME,CONVERT(VARCHAR(10),DATEADD(DAY,-1,DATEADD(MONTH,1,@date)),101))
END
ELSE
RETURN 1
RETURN @date
END

/*
Here is an example of how to invoke it (be sure to prefix the function call with dbo.)

SELECT GETDATE() AS "Current Date",
dbo.fx_LastDayOfMonth (GETDATE()) AS "Last Day Of Month"
*/

rrb
SQLTeam Poet Laureate

1479 Posts

Posted - 2002-02-03 : 21:26:42
And for First Day of Month

CREATE FUNCTION dbo.fx_FDOM
(@dDate datetime)
RETURNS datetime
AS
BEGIN
SET @dDate = DATEADD(day,-DAY(@dDate)+1,@dDate)

RETURN @dDate

END

--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"
Go to Top of Page
   

- Advertisement -