sql end of month


I have a list of dates for example,


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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Qlemo"Batchelor", 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?
Rey Obrero (Capricorn1)Commented:
try this query

select [datefield] from tablex
where [datefield]=dateserial(year([datefield]),month([datefield])+1,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
I am just curious...

Does the solution work for December?
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.
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.