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
camper12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
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

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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?
0
Rey Obrero (Capricorn1)Commented:
try this query

select [datefield] from tablex
where [datefield]=dateserial(year([datefield]),month([datefield])+1,0)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcelbaCommented:
I am just curious...

Does the solution work for December?
0
Rey Obrero (Capricorn1)Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.