compare 6 month old data between 2 tables

  • Hi there!

    I want to compare values from two tables but only values which are 6 month old.

    So I'm using this query:

    select * from Table1 as a

    EXCEPT

    select * from Table2 as b

    where ((datepart(dd,b.datefield) <= datepart(dd,GETDATE()))and

    (datepart(mm,b.datefield) <= datepart(mm,dateadd(mm,-6,GETDATE()))) and

    (datepart(yy,b.datefield) <= datepart(yy,GETDATE())))

    The clause 'where' appears to do nothing, so it returns all values. What seems to be the

    problem?

    Thanks for any help.

  • Hi once again!

    Forget about the last post. I've found the solution.

    The clause 'where' needs to be on both tables, like this:

    select * from Table1 as a

    where ((datepart(dd,a.datefield) <= datepart(dd,GETDATE())) and

    (datepart(mm,a.datefield) <= datepart(mm,dateadd(mm,-6,GETDATE()))) and

    (datepart(yy,a.datefield) <= datepart(yy,GETDATE())))

    EXCEPT

    select * from Table2 as b

    where ((datepart(dd,b.datefield) <= datepart(dd,GETDATE())) and

    (datepart(mm,b.datefield) <= datepart(mm,dateadd(mm,-6,GETDATE()))) and

    (datepart(yy,b.datefield) <= datepart(yy,GETDATE())))

    😀

  • would something like this work?

    select * from Table2 as b

    where (b.datefield) <= convert(datetime, convert(varchar,getdate(),101) + ' 23:59:59.997'))

    as it wouldn't be running a function on a column and atleast gives you the option of using an index

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply