Link to home
Start Free TrialLog in
Avatar of romeiovasu
romeiovasu

asked on

sql 2005 date query

i have a query written which executes everday night at 3.00am i want to pull for the whole month. i have a small problem over here, when it goes to next month it takes that particular month but i want the previous month. how can i do that.

eg: everday it generates total purchases for that month. but when it goes on next month first it give me 0 records because there is no purchase orders generated yet for that month. how can i over come this problem.
SELECT  st.storeid,
		st.storename,
        gp.description,
        rp.quantity,
        rp.unitcost
FROM    linkx.dbname.dbo.iqclerk_purchaseorders po
        INNER JOIN linkx.dbname.dbo.iqclerk_receiving ir ON ir.purchaseorderid = po.purchaseorderid
        INNER JOIN linkx.dbname.dbo.iqclerk_receivingandproducts rp ON ir.receivingid = rp.receivingid
        INNER JOIN linkx.dbname.dbo.iqclerk_stores st ON ir.receiverstoreid = st.storeid
        INNER JOIN linkx.dbname.dbo.iQclerk_GlobalProducts gp ON rp.globalproductid = gp.globalproductid
WHERE   vendorid = '22'
        AND LEFT(gp.categorynumber, 6) = '101010'
      	AND MONTH(ir.datereceived) = month(GETDATE())
        AND year(ir.datereceived) = year(GETDATE())

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
Avatar of romeiovasu
romeiovasu

ASKER

Thanks a lot for your angellll you have saved me so many times.
you are welcome!