Solved

sql 2005 date query

Posted on 2008-06-21
3
164 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
Comment Utility
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
Comment Utility
Thanks a lot for your angellll you have saved me so many times.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
you are welcome!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now