Solved

sql 2005 date query

Posted on 2008-06-21
3
167 Views
Last Modified: 2010-03-19
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

0
Comment
Question by:romeiovasu
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21838339
here we go.
note: I amended the query in some aspects in attempting to make it better (in terms of performance)
DECLARE @start_date DATETIME
SET @start_date = CONVERT(datetime, CONVERT(varchar(10), dateadd(day,-1, getdate()), 120), 120)
SET @start_date = DATEADD(day, 1-DATEPART(day, @start_date), @start_date 
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 gp.categorynumber LIKE '101010%'
  AND ir.datereceived >= @start_date 
  AND ir.datereceived < DATEADD(month, 1, @start_date)

Open in new window

0
 

Author Comment

by:romeiovasu
ID: 21838741
Thanks a lot for your angellll you have saved me so many times.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21838745
you are welcome!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question