SQL - First Month of Year and Last day of previous month.

I have a need to pull the first month of current year and then also pull the previous month end.

Example:  

1/1/2013 - 7/31/2013

I'm doing a parameter with a report and need to have this date automatically populated.  I am able to get the end of month for last month.  However, I can't seem to get the first month for the year at beginning of month.
holemaniaAsked:
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.

sdstuberCommented:
select trunc(sysdate,'yyyy'), trunc(sysdate,'mm') - 1 from dual
0
PortletPaulfreelancerCommented:
wanting "last day of month" is often associated with selection of date ranges

and further this is also often due to use of "between ...  and ..."

a simpler (and more reliable) date range selection method is to use  
a combination of >= with <

so instead of "end of month for last month" you can use "less than 1st of this month" e.g.
-- Oracle syntax
select
*
from <<whatever>>
where ( date_field >=  trunc(sysdate,'yyyy') and date_field < trunc(sysdate,'mm') )
and ...

-- MS SQL syntax
select
*
from <<whatever>>
where ( date_field >=  dateadd(YEAR, datepart(YEAR,getdate())-1900, 0)
       and date_field < dateadd(DAY, -(DAY(getdate())- 1), dateadd(DAY, datediff(DAY,0, getdate() ),0))
             )
and ...

Open in new window

for more on the topic of "between" please see: "Beware of Between"

fyi: in mssql for last day of last month:
dateadd(DAY, -(DAY(getdate())), dateadd(DAY, datediff(DAY,0, getdate() ),0))

if the need isn't for either Oracle/SQL Server please identify dbms type/version
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
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
Query Syntax

From novice to tech pro — start learning today.