Solved

sql 2005 date query

Posted on 2008-06-21
3
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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