Link to home
Start Free TrialLog in
Avatar of damixa
damixaFlag for Denmark

asked on

Binary query to see if a date exists in a table

I have to tables

table1 and table2

each of these tables has date1 and date2 respectively.

I have a process where I append the daily records from table2 to table1. I want to make sure they are not duplicated.

to do this I was thinking on checking and seeing that any of the dates in table2 exists in table 1

If any of the dates in table2.date2 exists in table1.date1

(Block select statement 1)
else
(block select statement2)

endif

I just need some help with the first if statement. How do I write the query so that it checks if any of the values in date2 exists in date1 giving me a true or false value.

thanks
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

This code will INSERT only rows that don't already exist.

INSERT INTO dbo.table1 (
    date1, ...
    )
SELECT
    t2.date2, ...
FROM dbo.table2 t2
LEFT OUTER JOIN dbo.table1 t1 ON
    t1.date1 = t2.date2
WHERE
    t1.date1 IS NULL AND
    t1.date1 >= '...' AND
    t2.date2 >= '...'
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Interesting ... I thought my earlier-posted answer was more complete.