Solved

Binary query to see if a date exists in a table

Posted on 2014-04-10
3
308 Views
Last Modified: 2014-04-10
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
0
Comment
Question by:damixa
[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 69

Expert Comment

by:Scott Pletcher
ID: 39992175
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 >= '...'
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39992183
INSERT INTO table1 (date1)
SELECT t2.date2
FROM table2 AS t2
LEFT OUTER JOIN table1 AS t1
   ON t2.date2 = t1.date1
WHERE t1.date1 IS NULL

Obviously the column lists need to be adjusted to include any additional columns which you have not mentioned.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39992751
Interesting ... I thought my earlier-posted answer was more complete.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

717 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