Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find last day of month

Status
Not open for further replies.

GreenFella

Programmer
Oct 23, 2001
41
CA
Is there a function available to determine the last day of a given month?

I've been searching and come up with nothing.

TIA
Greenfella
 
Try this:

Code:
DECLARE @date datetime,
	@last_month_day datetime

SET @date = '2005-04-20'
SET @last_month_day = CAST(CONVERT(varchar(6), DATEADD(mm, 1, @date), 112) + '01' AS datetime) - 1

SELECT @date, @last_month_day

--James
 

If you create a function which uses the principal 1st of each month is always 01/mm/yyyy. then just use
Code:
dateadd(d, -1, mydatefield)
will post a function but not sure what parameter you require.

"I'm living so far beyond my income that we may almost be said to be living apart
 
Another way (feat. donutman):

Code:
SET @last_month_day = dateadd(mm, 1+datediff(mm, 0, @date), 0) -1

------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top