Hi there,
In one of my field I have got records showing attribute
1) A1,F4,A10
2) A1,A17
3) F1,G6, A1
4) A10, A1
6) D1, A17
7) D2, D4
and so on I am trying to select records showing A1 valueI have tried using query Field1 LIKE '%A1%' it is also selecting A10. Is there any way I can select only value A1?
Thanks,
Anu
Solved! Go to Solution.
As Vince already pointed out, your data design doesn't fit well within the relational model, which makes storing and querying it in a relational data store kludgy. That said, it is not uncommon to see this kind of data design, and most relational data stores and the SQL standard itself have made some accommodations.
Trying to use SQL LIKE to accomplish your matching will result in a long list of LIKE and OR statements. Depending on your backend data store, you can use expanded pattern matching through LIKE or SIMILAR. The not-so-good news is that expanded pattern matching is very implementation/product specific, so portability suffers.
If you are working with file geodatabases, see if the following works:
-- SIMILAR TO syntax here only works correctly in
-- ArcGIS Desktop 10.6 or greater and ArcGIS Pro 2.0 or greater
field_name SIMILAR TO '(A1[ ,]%|%[ ,]A1[ ,]%|%[ ,]A1)'
Looking closely at your data, the matching is made more difficult by inconsistent use of spaces. For some records, there is a space after a comma but not all the time. My SQL above assumes there may or may not be a single space or comma before or after the value you are searching.
UPDATE:
The comparable SQL to my above using basic pattern matching with LIKE is:
field_name LIKE 'A1,%' OR
field_name LIKE 'A1 %' OR
field_name LIKE '% A1 %' OR
field_name LIKE '% A1,%' OR
field_name LIKE '%,A1 %' OR
field_name LIKE '%,A1,%' OR
field_name LIKE '% A1' OR
field_name LIKE '%,A1'
The SQL code using LIKE is very portable, but as you can see, you shouldn't expect much for performance when you chaining together so many condition checks using OR.
drop the 2nd %
Field1 LIKE '%A1'
Unfortunately, dropping the second wildcard will end up missing cases where A1 is not at the end of the string.
Ahhhh he didn't indicate that possibility
Just going with the KISS principle first
This is why lists are not considered best-practice in database design.
If you had a second table organized:
keycol | attrval |
---|---|
1 | A1 |
1 | F4 |
1 | A10 |
2 | A1 |
2 | A17 |
3 | F1 |
3 | G6 |
3 | A1 |
4 | A10 |
4 | A1 |
then you could fashion a query:
SELECT *
FROM mytable
WHERE keycol in (
SELECT keycol
FROM secondtab
WHERE attrval = 'A1')
- V
Hi Vince,
Unfortunately this is third party data and I have to process it without doing any changes.
Thanks ,
Anu
As Vince already pointed out, your data design doesn't fit well within the relational model, which makes storing and querying it in a relational data store kludgy. That said, it is not uncommon to see this kind of data design, and most relational data stores and the SQL standard itself have made some accommodations.
Trying to use SQL LIKE to accomplish your matching will result in a long list of LIKE and OR statements. Depending on your backend data store, you can use expanded pattern matching through LIKE or SIMILAR. The not-so-good news is that expanded pattern matching is very implementation/product specific, so portability suffers.
If you are working with file geodatabases, see if the following works:
-- SIMILAR TO syntax here only works correctly in
-- ArcGIS Desktop 10.6 or greater and ArcGIS Pro 2.0 or greater
field_name SIMILAR TO '(A1[ ,]%|%[ ,]A1[ ,]%|%[ ,]A1)'
Looking closely at your data, the matching is made more difficult by inconsistent use of spaces. For some records, there is a space after a comma but not all the time. My SQL above assumes there may or may not be a single space or comma before or after the value you are searching.
UPDATE:
The comparable SQL to my above using basic pattern matching with LIKE is:
field_name LIKE 'A1,%' OR
field_name LIKE 'A1 %' OR
field_name LIKE '% A1 %' OR
field_name LIKE '% A1,%' OR
field_name LIKE '%,A1 %' OR
field_name LIKE '%,A1,%' OR
field_name LIKE '% A1' OR
field_name LIKE '%,A1'
The SQL code using LIKE is very portable, but as you can see, you shouldn't expect much for performance when you chaining together so many condition checks using OR.