Subject | SQL Puzzle: Two Way String Matching |
---|---|
Author | red_october2009 |
Post date | 2012-05-14T18:46:09Z |
I have a table with an ID number, and two string fields, one for family human names and one for family pet names. The names are delimited by space character.
USR_ID INTEGER (primary key, field 1)
ID_NMBR INTEGER (primary key, field 2)
MY_FAMILY_HUMAN_NAMES VARCHAR(150)
MY_FAMILY_PET_NAMES VARCHAR(150)
Data:
The ^ denotes field boundaries.. not part of the data
Extra spaces are for readability only.. not part of the data
1 ^ 185 ^ RALF JOEY FRED ^ BRUTUS
1 ^ 105 ^ KARL JACOB NEWTON ^ SNACKER
1 ^ 973 ^ MARLEEN MUFFY ABE ^ LUVER POPEYE TANGO
2 ^ 243 ^ BAXTER MILTON MARY ^ RIPPER JOEY TOMMY
2 ^ 239 ^ ABIGAIL HARMONY ^ SPOT MUFFY TIPPY
2 ^ 982 ^ KEVIN PETE PAT ^ THOR TINKLES KELSEY
I need an SQL statement that will return all records where at least one human name is found in the pet names field (not the other way around). In this case the only records that qualify are:
2-243 (JOEY)
2-239 (MUFFY)
Anyone got any ideas? Every time I attempt this, it gets overly complicated.
USR_ID INTEGER (primary key, field 1)
ID_NMBR INTEGER (primary key, field 2)
MY_FAMILY_HUMAN_NAMES VARCHAR(150)
MY_FAMILY_PET_NAMES VARCHAR(150)
Data:
The ^ denotes field boundaries.. not part of the data
Extra spaces are for readability only.. not part of the data
1 ^ 185 ^ RALF JOEY FRED ^ BRUTUS
1 ^ 105 ^ KARL JACOB NEWTON ^ SNACKER
1 ^ 973 ^ MARLEEN MUFFY ABE ^ LUVER POPEYE TANGO
2 ^ 243 ^ BAXTER MILTON MARY ^ RIPPER JOEY TOMMY
2 ^ 239 ^ ABIGAIL HARMONY ^ SPOT MUFFY TIPPY
2 ^ 982 ^ KEVIN PETE PAT ^ THOR TINKLES KELSEY
I need an SQL statement that will return all records where at least one human name is found in the pet names field (not the other way around). In this case the only records that qualify are:
2-243 (JOEY)
2-239 (MUFFY)
Anyone got any ideas? Every time I attempt this, it gets overly complicated.