SQL Query

What does the query below gives you? Is it giving everything for the previous month?

Where SUBSTRING(CONVERT(varchar, DATEADD(m,-1,getDate()), 112),1,6) = SUBSTRING(CONVERT(varchar, RSIH.RSIH_DATE, 112),1,6)
LVL 1
isamesAsked:
Who is Participating?

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

x
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.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, all data for the previous month. But it is a bad way to do it - a between would be much better,  performance wise.
YZlatCommented:
you could do

Where Month(DATEADD(m,-1,getDate())) = Month(RSIH.RSIH_DATE) AND Year(DATEADD(m,-1,getDate())) = Year(RSIH.RSIH_DATE)

Open in new window

MlandaTCommented:
I agree with QLemo. That method would ignore any indexes on that date column. This is better:
DECLARE @FirstDayThisMonth DATETIME
SET @FirstDayThisMonth = DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)

SELECT *
FROM RISH
WHERE RSIH.RSIH_DATE >= DATEADD(month, -1, @FirstDayThisMonth) 
      AND RSIH.RSIH_DATE < @FirstDayThisMonth

Open in new window

or without the variable
SELECT *
FROM RISH
WHERE RSIH.RSIH_DATE >= DATEADD(month, -1, DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0) ) 
      AND RSIH.RSIH_DATE < DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)

Open in new window

OR if you want to use the new EOMONTH function (SQL Server 2012+)
SELECT *
FROM data_table
WHERE data_table.date_column >= DATEADD(DAY,1,EOMONTH(GETDATE(),-2)) --beginning of last month 
 and data_table.date_column < DATEADD(DAY, 1, EOMONTH(GETDATE(),-1)) --beginning of this month

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The question was "What does the query below gives you? Is it giving everything for the previous month?". The answer by MlandaT is showing a different and much better way, but has not been asked for, so I cannot agree to the way the question got closed. An equal split might be acceptable, though.
MlandaTCommented:
I must agree with QLemo on this one. He answered the question more directly, though I built on it with actual examples and alternative syntaxes that might be easier to read -  EOMONTH. I do think examples to back up a perspective are often better than just a short direct answer. So yes.. Equal split perhaps.

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
Microsoft SQL Server

From novice to tech pro — start learning today.