0

I have been searching now for hours.

Got a String like: "_1_11_19_12_17_21_41_111_100_1"

Now, I want the Output to be a single 1

When I try:

"SELECT * FROM jobs_tb  WHERE position_id LIKE '%1%' "

This prints all most everything:

I tried:

SELECT *
FROM `jobs_tb`
WHERE (
`position_id` LIKE '%_1%'
)
OR (
`position_id` LIKE '%_1_%'
)

All most the same as the previous.

How do you isolate a 1 when 1s are mixed with other numbers?

Any suggestion is highly welcomed

4
  • 1
    This task isn't really well-suited to SQL. Is there any way you could perform this task from your application code (PHP?)? Commented Dec 6, 2014 at 6:42
  • Also, if you expect to have to parse this data from within SQL frequently, I suggest storing the logic in a user function (CREATE FUNCTION) so you don't have to repeat the logic in other statements. Commented Dec 6, 2014 at 6:43
  • Any Idea how?... thx alot for the hint, please if ok, try to elaborate in an answer... thax again Commented Dec 6, 2014 at 6:44
  • @GolezTrol that's my interpretation of the question title, but the question body suggests he just wants to use it as a predicate in a WHERE clause (see my answer). Commented Dec 6, 2014 at 6:53

2 Answers 2

2

Note that an underscore is a single character wildcard, just as the percent sign is a multicharacter wildcard, so if you want to match a literal underscore, you'll have to escape it. Also, the string can start or end with a '1', so you need to take that into account. A simple trick is to add extra underscores around the value to search in. Try this query:

SELECT *
FROM `jobs_tb`
WHERE
  CONCAT('_', `position_id`, '_') LIKE '%\_1\_%'
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like VOODOO!... Thanks a million...(Accepted)
1
CREATE FUNCTION PositionIdStringContains(positionIdText TEXT, digit TEXT) RETURNS BIT
BEGIN
    DECLARE digitStart TEXT;
    SET digitStart = CONCAT( digit, '\_%' );
    DECLARE digitEnd TEXT;
    SET digitEnd = CONCAT( '%\_', digit );
    DECLARE digitMid TEXT;
    SET digitMid = CONCAT( '%\_', digit, '\_%' );

    RETURN
        positionIdText LIKE digit OR
        positionIdText LIKE digitStart OR
        positionIdText LIKE digitMid OR
        positionIdText LIKE digitEnd
END;

Then use it in your SELECT statements like so:

SELECT
    *
FROM
    jobs_tb
WHERE
    PositionIdStringContains( position_id, '1' )

4 Comments

Thnks alot... though I get an Error: [--#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 --]
@UniversalGrasp that's probably because of my use of the ; delimiter. You will need to configure your MySQL client accordingly. That's not something I can help you with.
Just to know, Will I have to be using this Function in Ever Query or the created function will automatically be stored in mySql memory forever?...Thanks again
CREATE FUNCTION stores the function in the database permanently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.