INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Finding one of an array of tokens in a particular string position
2

Finding one of an array of tokens in a particular string position

Finding one of an array of tokens in a particular string position

(OP)
I could use some guidance to select all products where the 2nd position of the string is equal to P or J or W or Z or G or any of another dozen defined letters. Is there a way to do this without just copying/pasting the LOCATE for each valid option in this example?

CODE

SELECT DISTINCT `Type` 
FROM products WHERE LOCATE("P", `Type`) = '2' OR LOCATE("J", `Type`) = '2' 
ORDER BY `Type` 

This could get clunky:
...OR LOCATE("P", `Type`) = '2'
OR LOCATE("J", `Type`) = '2'
OR LOCATE("W", `Type`) = '2'
OR LOCATE("Z", `Type`) = '2'
OR LOCATE("G", `Type`) = '2'...

Is there a better way?

RE: Finding one of an array of tokens in a particular string position

Hi

Somehow similar :

CODE --> MySQL

SELECT DISTINCT `Type`

FROM products

WHERE substring(`Type`, 2, 1) in ('P', 'J', 'W', 'Z', 'G')

ORDER BY `Type` 

Note that this is not exactly the same as your original. This will return true for a `Type` value of 'PP-blah', while your original returns false in this case. See whether this is an issue for you.

Feherke.
feherke.ga

RE: Finding one of an array of tokens in a particular string position

(OP)
Thanks! My original sample would indeed give me an unwanted result for 'PP-blah'.

I don't know what insanity led me to tinker with locate as I should have stuck to my original attempts with substring.

RE: Finding one of an array of tokens in a particular string position

you are also be able to use regex. but with simple comparisons I doubt whether this would give you any performance benefit over the substring method.

CODE

SELECT DISTINCT `Type`
FROM products
WHERE `Type` REGEXP '^.[PJWZG].*$'
ORDER BY `Type` ASC 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close