Solved

Binary query to see if a date exists in a table

Posted on 2014-04-10
3
287 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
  • 2
3 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
ID: 39992751
Interesting ... I thought my earlier-posted answer was more complete.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

15 Experts available now in Live!

Get 1:1 Help Now