Solved

sql 2005 date query

Posted on 2008-06-21
3
166 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 142

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 142

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help  needed 3 26
TSQL query to generate xml 4 35
SQL Error - Query 6 26
Microsoft Access Delete all Records from table but Max 2 6
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

810 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