Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Comparing data strings using "Where not exists"

Author  Topic 

nelsont
Starting Member

25 Posts

Posted - 2013-01-09 : 19:47:01
A while ago I posted in here because I was doing an insert into a table based on using "Not In" to see if the data already existed in the target table. I kept getting the same results in my query when I switched the "Not In" to "In" because of the way SQL handles nulls. It was suggested that I use "not exists" or "exists" instead.

I am trying to get this to work and I get the same results whether I use = or <>. None of these fields are nullable. The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.


This is driving me crazy. The 'where not exists' has worked fantastically in all of the other parts of my SSIS routine, but none of those are using combinations of fields that are all int - they are a mix of data types.

Any ideas?


insert into tableA (
field1,
field2,
field3)
select
b.field1,
c.field2,
d.field3
from tableB b
left join tableC c on b.id = c.id
left join tableD d on b.id = d.id
where not exists(
select null from tableA where
cast(field1 as varchar(20))+
cast(field2 as varchar(20))+
cast(field3 as varchar(20)))
=
cast(b.field1 as varchar(20))+
cast(b.field2 as varchar(20))+
cast(b.field3 as varchar(20)))
)





I have also tried hashing each group of fields on either side of the = sign to no avail. I'm just stuck.
Any help would be greatly appreciated.

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-01-09 : 20:18:10
Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.
Go to Top of Page

nelsont
Starting Member

25 Posts

Posted - 2013-01-09 : 21:58:07
do you mean instead of what I have in the not exists clause? where should your code go in my insert statement?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-09 : 22:41:32
quote:
Originally posted by LoztInSpace

Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.



i dont think that will work in this case as OPs requirement states emphasis is on concatenated result and not on individual values. See below statement

The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-01-10 : 00:33:37
quote:
Originally posted by visakh16

quote:
Originally posted by LoztInSpace

Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.



i dont think that will work in this case as OPs requirement states emphasis is on concatenated result and not on individual values. See below statement

The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




I interpreted that to mean "I can only use EXISTS on one column but my condition is 3 columns so I stuck them together. As they are numbers, a mathematical sum would not be sufficiently unique".
Of course if 10, 11 & 12 should match 101, 1 and 12 then I am wrong.
However, assuming my interpretation:
quote:
Originally posted by nelsont
do you mean instead of what I have in the not exists clause? where should your code go in my insert statement?


Yes. It needs to go in the select:


...
from tableB b
left join tableC c on b.id = c.id
left join tableD d on b.id = d.id
where not exists(
select null from tableA where
cast(field1 as varchar(20))+
cast(field2 as varchar(20))+

LEFT OUTER JOIN tableA on
b.field1=tableA.field1 and
b.field2=tableA.field2 and
b.field3=tableA.field3
WHERE tableA.field1 IS NULL


If visakh16's interpretation is right then just join on the concatenated strings:

LEFT OUTER JOIN tableA on
b.field1+b.field2+b.field2 =
tableA.field1+tableA.field2+tableA.field2
WHERE tableA.field1 IS NULL

(with casts but I'm too lazy to type all that in ).
Bear in mind the concatenation will preclude the use of indexes so you'll get a different plan that may be slower.
Go to Top of Page

nelsont
Starting Member

25 Posts

Posted - 2013-01-10 : 13:11:22
The left outer join worked. Thank you for the help. The problem is that I don't understand why.

I thought that the way I was doing it was sound, just not preferred. Is there something inherently wrong with the way I had it set up?
Go to Top of Page

jamesbrummel
Starting Member

5 Posts

Posted - 2013-01-10 : 14:08:04
NOT or <> etc tend to be pretty slow, better to find your matches first, what's left is the NOTs, which is what a LEFT join does.

Most people who drown do so in chest deep water.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-10 : 22:50:27
quote:
Originally posted by nelsont

The left outer join worked. Thank you for the help. The problem is that I don't understand why.

I thought that the way I was doing it was sound, just not preferred. Is there something inherently wrong with the way I had it set up?


So was your requirement to match on individual column names? your initial explanation sounded as if you're interested in concatenated result

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nelsont
Starting Member

25 Posts

Posted - 2013-01-11 : 04:22:48
It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-11 : 06:04:49
quote:
Originally posted by nelsont

It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.


ok..that makes it clear

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-01-11 : 10:02:21
quote:
Originally posted by nelsont

It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.


The same can be said for the strings:
10,20,30 also matches
10,203,0
or
1020,3,0
so watch out for these kludges.
Go to Top of Page
   

- Advertisement -