Page 1 of 1

last day of the month of a given date

Posted: Wed May 20, 2009 11:57 pm
by Swaruparani
i will pass a date an input ,i need to get the last day of that month.

EX:if i will pass 2009-02-16 as input then my expected output should be 28(last day of FEB)

Thanks in advance.

Posted: Thu May 21, 2009 12:05 am
by ray.wurlod
Welcome aboard.

This is a perfect example of something most easily resolved using a time "dimension" (lookup) table at day granularity.

Posted: Thu May 21, 2009 3:34 am
by priyadarshikunal
ray.wurlod wrote:This is a perfect example of something most easily resolved using a time "dimension" (lookup) table at day granularity.
Yes, It is. But if its a straight forward requirement then transforming it to 1st of next month -1 day is not a bad idea.

Posted: Thu May 21, 2009 8:48 am
by dsedi
Search is your friend :)

you can do it via oracle procedure too

CREATE FUNCTION dbo.LastMonthDay
( @Date datetime )
RETURNS datetime
AS
BEGIN
RETURN (CASE WHEN MONTH(@Date)= 12
THEN DATEADD(day, -1, CAST('01/01/' + STR(YEAR(@Date)+1) AS DateTime))
ELSE DATEADD(day, -1, CAST(STR(MONTH(@Date)+1) + '/01/' + STR(YEAR(@Date)) AS DateTime))
END)
END
GO

Posted: Thu May 21, 2009 9:10 am
by priyadarshikunal
If you can use oracle query then you don't need a procedure to do the same. there is a function called Last_day() in oracle which gives you last day of the month without any effort.

Posted: Thu May 21, 2009 4:27 pm
by ray.wurlod
Where did the original question mention Oracle?
:roll:

Posted: Fri May 22, 2009 3:00 am
by Sainath.Srinivasan
Can do something like
endOfMonthDate = YYYY + INT(MM / 12) : MOD(MM, 12) + 1 : 01 - 1

Posted: Fri May 22, 2009 4:30 am
by priyadarshikunal
ray.wurlod wrote:Where did the original question mention Oracle?
:roll:
I was just replying to the post above suggesting to use a oracle procedure.

Posted: Fri May 22, 2009 5:56 am
by ray.wurlod
What if they're a DB2 shop? You've been in Ireland too long!

Posted: Fri May 22, 2009 6:31 am
by priyadarshikunal
What if they're a DB2 shop?
Then they must have started searching for me. :wink:
You've been in Ireland too long!
Yes.
Something wrong with the language or nature of reply?
then I must start thinking on that.

Posted: Fri May 22, 2009 6:47 am
by priyadarshikunal
I just figured out your opinion about Ireland. :wink:

Posted: Fri May 22, 2009 4:04 pm
by ray.wurlod
There is a very old joke, though many say it's true, that goes something like this. A visitor to Ireland asks directions to a particular attraction, and gets the reply "well, if I were going there, I wouldn't be starting from here".