Link to home
Start Free TrialLog in
Avatar of camper12
camper12

asked on

sql end of month

Hi,

I have a list of dates for example,

1/1/2002
1/2/2002....
31/1/2002..
1/2/2003
2/2/2003..
2/27/2003

I want to create a temp table and select only those dates that are end of month for each year. I could have used
select DATEADD(DAY,-1, '3/1/2003')) but this would have returned me 2/28/2003 but I need to get 2/27/2003
How can i do this in sql. Can i use loop in sql?

Thanks
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

You want the real month end date or the max date in the month?

select max(date) from YourTable group by YEAR(Date), Month(Date)

Open in new window

31/1/2002  is certainly a typo ...

It looks like you need the last row found for each month. Or is it only the date itself?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am just curious...

Does the solution work for December?
why don't you try, substitute any date in december in the [datefield] and found out the result for yourself.

trying is one way of learning.