Solved

Date Range Bug: If I give param as 08/01/2004 and 08/30/2004 no results but If I give param as 10/01/2004 and 10/30/2004 the results are displaying with this query.

Posted on 2004-10-26
212 Views
Last Modified: 2006-11-17
I have a query below, for that IF I provide parameters as 08/01/2004 and 08/30/2004 no results but If I give parameters as 10/01/2004 and 10/30/2004 the results are displaying with this query.
May be I am doing something wrong in DARTEADD FUNCTION. Can you help me resolve this problem in this below query

CREATE  PROCEDURE usp_test

@BeginDate DateTime,
@EndDate DateTime

AS

SELECT P.DocID, count(pageID) as Pages
FROM tb_Page P
INNER JOIN tb_Doc D on P.DocID = D.DocID
WHERE P.CreateDate >= @BeginDate
AND P.CreateDate < dateadd(day, 1, @EndDate)
AND D.Status > 0
GROUP BY P.DocID
HAVING COUNT(PageID) > 6
GO

Thanks
Mike
0
Question by:MikeHall12
    8 Comments
     
    LVL 6

    Assisted Solution

    by:Duane Lawrence
    Your asking for opposites:

    WHERE P.CreateDate >= @BeginDate
    AND P.CreateDate < dateadd(day, 1, @EndDate)

    Note the >= and < are on the same variables, this will exclude everything.

    Duane
    0
     
    LVL 68

    Assisted Solution

    by:ScottPletcher
    Is the column CreateDate a DATETIME datatype?

    Are you *sure* there are records matching August in the tables?  Maybe it's not returning records because there really aren't any matching ones :-) .  What result does this give?:

    SELECT COUNT(*)
    FROM tb_Page
    WHERE CreateDate >= '08/01/2004' AND CreateDate < DATEADD(DAY, 1, '08/30/2004')


    Btw, did you want 08/31/2004 or 08/30/2004?
    0
     

    Author Comment

    by:MikeHall12
    Scott,
    With the below query that you mentioned does return 0 output
    SELECT COUNT(*)
    FROM tb_Page
    WHERE CreateDate >= '08/01/2004' AND CreateDate < DATEADD(DAY, 1, '08/30/2004')

    but there are records in DB with '08/01/2004'  and '08/30/2004'
    If I put the parameters 07/20/2004 to 10/30/2004, I am getting records .

    Thanks for the reply
    Mike
    0
     

    Author Comment

    by:MikeHall12
    duane,
    you are saying that the variable is same that will exclude everything.
    but How come I am returning records with this parameters 10/01/2004 and 10/30/2004.
    Please help me resolve.
    Mike
    0
     
    LVL 32

    Assisted Solution

    by:bhess1
    Do me a favor.  Execute the following, and show the output as shown in Query Analyzer:

    SELECT
     Top 10  'Type1' as Typ, CreateDate
     FROM (Select CreateDate FROM tbl_Page
     WHERE CreateDate >= '20040801' Order By CreateDate) Type1
    UNION
     Top 10  'Type2' as Typ, CreateDate
     FROM (Select CreateDate FROM tbl_Page
     WHERE CreateDate >= '08/01/2004' Order By CreateDate) Type2
    0
     
    LVL 32

    Assisted Solution

    by:bhess1
    Better yet - show the results of this query:
    SELECT CreateDate, Count(*)
    FROM tbl_Page
    WHERE CreateDate >= '08/01/2004'
    Group By CreateDate
    Order By CreateDate
    0
     
    LVL 32

    Accepted Solution

    by:
    DUH!  I hate doing that.

    The problem seems to be that there is not 6+ pages in the given range.  And, since I did not filter off the times, those queries will not actually produce any good results.  Try:

    SELECT P.DocID, Count(PageID) As Pages, Month(CreateDate) As Mth
    FROM tbl_Page P
    INNER JOIN tb_Doc D
        on P.DocID = D.DocID
    WHERE P.CreateDate >= '08/01/2004'
    AND D.Status > 0
    Group By Month(CreateDate), p.Docid
    Order By Month(CreateDate), Count(PageID) Desc
    0
     

    Author Comment

    by:MikeHall12
    I found the problem by myself. CreateDate is updated in Database, so that is the reason it is not displaying values.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    875 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

    13 Experts available now in Live!

    Get 1:1 Help Now