views:

35

answers:

1

Here's a question for all those SQL SERVER 2000 experts:

I have only 1 table... I can already find if any of the values in a certain field, also appears in another record.

I.E.: Does any record have "ABCDEFGHI" in a field, and then "ABCDEFGHI" again in that same field... but in another record.

But I run into trouble when I try to use substrings.

I.E.: Does any record have "CDEF" in a field, and then "DEFG" again in that same field... but in another record. (Edit: That would NOT be a match, of course.)

I'm trying to compare PART of 1 field, with PART of another. Only compare characters 3-6 characters, with characters 4-7. (I need to specify my own start-end ranges, for both fields.) What the specific letters are... doesn't matter. Just that they 'match'.

This doesn't seem to work:

SELECT t1.ID + ' + ' + t2.ID  
FROM InfoTable As t1         
INNER JOIN InfoTable AS t2     ON t1.ID = SUBSTRING(t2.ID, 3, 4) 

(Edit: I also need to NOT list any records that are just matching themselves.)

+1  A: 

Perhaps

SELECT t1.ID + ' + ' + t2.ID  
FROM InfoTable As t1         
INNER JOIN InfoTable AS t2     ON SUBSTRING(t1.ID,3,6) = SUBSTRING(t2.ID, 4, 7) 
Eton B.
I tried that, and it seems to return records that match themselves. Is there a way to exclude those 'same record' matches?
HelenJ
add another condition to the ON clause...and t1.id < t2.id
G Mastros
Simply add `AND T1.ID <> T2.ID` to the WHERE clause. That's assuming that ID is unique. It would probably be best if you gave a table definition, sample data, and expected results - that's always a good idea when posting SQL query questions.
Tom H.
Some great ideas. Let me try them. Are either of these "better": "Add < to the ON clause"... or... "Add <> in the WHERE clause"? (To avoid records matching themselves.)
HelenJ
@Helen: I think having it in the ON clause is better, because it makes it clear that you don't want to link to the same record. (Processing-wise, in this query it shouldn't make any difference.) Using `<>` would enable the same two records to appear twice with each other, if **both** SUBSTRING(t1.ID,3,6) = SUBSTRING(t2.ID, 4, 7) **and** SUBSTRING(t2.ID,3,6) = SUBSTRING(t1.ID, 4, 7) - on the other hand, if only the latter is true, then using `<` would mean that neither would appear. I suggest that `<>` is therefore better.
Mark Bannister