dbTalk Databases Forums  

I need to find the rows that exist in one table but not in the other with condition

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss I need to find the rows that exist in one table but not in the other with condition in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
yuval
 
Posts: n/a

Default I need to find the rows that exist in one table but not in the other with condition - 06-20-2007 , 03:28 AM






I need to find the rows that exist in one table but not in the other
with this condition:

(prod_name exist in table1 and not in table2.prod_name ) AND


(prod_name exist in table1 and not in table2.'S'+prod_name )


explanation:
i want to know if the product not exit and if the combination of the
charachter "S" with the product Name also not exist at the other
table


B.R
yuvi


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: I need to find the rows that exist in one table but not in the other with condition - 06-20-2007 , 04:48 AM






SELECT prod_name
FROM table1 as A
WHERE NOT EXISTS
(select * from table2 as B
where A.prod_name = B.prod_name)
AND NOT EXISTS
(select * from table2 as C
where A.prod_name = 'S' + C.prod_name)

Roy Harvey
Beacon Falls, CT

On Wed, 20 Jun 2007 01:28:26 -0700, yuval <yuvalbra (AT) gmail (DOT) com> wrote:

Quote:
I need to find the rows that exist in one table but not in the other
with this condition:

(prod_name exist in table1 and not in table2.prod_name ) AND


(prod_name exist in table1 and not in table2.'S'+prod_name )


explanation:
i want to know if the product not exit and if the combination of the
charachter "S" with the product Name also not exist at the other
table


B.R
yuvi

Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: I need to find the rows that exist in one table but not in the other with condition - 06-20-2007 , 03:55 PM



On Wed, 20 Jun 2007 01:28:26 -0700, yuval wrote:

Quote:
I need to find the rows that exist in one table but not in the other
with this condition:

(prod_name exist in table1 and not in table2.prod_name ) AND


(prod_name exist in table1 and not in table2.'S'+prod_name )


explanation:
i want to know if the product not exit and if the combination of the
charachter "S" with the product Name also not exist at the other
table
Hi yuvi,

Some alternatives for the solution posted by Roy. Try to see if they run
faster or slower on your system.

SELECT prod_name
FROM table1 AS a
WHERE NOT EXISTS
(SELECT *
FROM table2 AS b
WHERE a.prod_name IN (b.prod_name, 'S' + b.prod_name));

(Only SQL2005)

SELECT prod_name
FROM table1
EXCEPT
SELECT prod_name
FROM table2
EXCEPT
SELECT 'S' + prod_name
FROM table2;

SELECT prod_name
FROM table1
EXCEPT
(SELECT prod_name
FROM table2
UNION ALL
SELECT 'S' + prod_name
FROM table2);

(All the queries above are untested - see www.aspfaq.com/5006 if you
prefer a tested reply).

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.