Link to home
Start Free TrialLog in
Avatar of adraughn
adraughnFlag for United States of America

asked on

Rolling Date Range

'morning. SQl 2k5. I need to modify my access query into an sp. i have started it, but am having issues with the date range. i need the range to be current month (partial or full) and prior 11 months. For example, today's report should reference: 5/01/06 to 4/09/07. Here is what I have:

(I got the date part from a prior post, but it isn't working as a rolling 12 month. It is returning Jun 2006 thru March 2007.)

ALTER PROCEDURE [dbo].[upOrders_WrntyExchsCY] AS
DECLARE @d datetime
SET @d = CONVERT(datetime, CONVERT(varchar(10), getdate(), 120), 120)
SET @d = dateadd(day, 1- datepart(day, @d ), @d )
SELECT ord.ServiceType, ord.OrderType, ord.ReportMonth,
CONVERT(CHAR(8), lmd.Report_M_Date, 10) AS Report_M_Date
FROM dbo.tblOrders AS ord LEFT OUTER JOIN dbo.tblLU_RptMDate AS lmd ON
ord.ReportMonth = lmd.ReportMonth WHERE (ord.OrderType <> 'PO') AND
(ord.OrderType <> 'SP') AND (lmd.Report_M_Date >= dateadd(month, -11, @d ))



Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

try

 AND (lmd.Report_M_Date >= dateadd('m', -11, @d ))
Avatar of adraughn

ASKER

that returned an error.

Invalid parameter specified for DateAdd (maybe because you are now passing 'm' as a string instead of month?)
adraughn,
sorry about that, thinking it was on access.
does SQL accept dateserial ?
i don't believe so, but i could be wrong. i have only used it in vb.

adria
is Report_M_Date in the same format as @d ?
yes, they are both dates.
that is not what i meant
mm/dd/yy is a date format
mm/dd/yyyy is another date format

are they both in this format  mm/dd/yy  ?
not exactly, but i don't see why that matters, because the calcualtion in SQLwill use the date field, no matter how you format it.
ASKER CERTIFIED SOLUTION
Avatar of adraughn
adraughn
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
In Access results varies if one of criteria in a query is a datefield and the datefield is not in the same format as the criteria.

see this thread
https://www.experts-exchange.com/questions/22496839/Product-Sales-by-Customer-over-date-peroid.html

and the selected answer

https://www.experts-exchange.com/questions/22496839/Product-Sales-by-Customer-over-date-peroid.html?cid=236&anchorAnswerId=18866018#a18866018

try using the same format in both sides.
well, thats good
this was all in SQL 2k5 - created for an access FE so the formatting issue does not apply. Thanks anyway for your resposnes.

adria