-1

I'm trying to parse model names from a table using LIKE. Each model value looks like new_model_01215, new_model_01557 etc. Where new_model_01 part will be the same for all values. And there will always be only 3 numbers after the 01.

I've created the following statement and it works almost as expected. But the problem is that it returns values having more than 5 digits in a postfix.

What should I change in the query to return values with no more than 3 digits after the 01%?

SELECT model_name FROM models_table WHERE model_name LIKE 'new_model_01%'
1

4 Answers 4

3

In postgresql you can use the _ (underscore) character to match a single character, so three in a row would match your strings:

    SELECT model_name FROM models_table WHERE model_name LIKE 'new_model_01___'
Sign up to request clarification or add additional context in comments.

2 Comments

The underscore will match not only digits but any char.
@forpas, you are correct that the wildcard matches any single character, but matching only digits was not the issue for the OP - only matching a specific number of digits, so I suggested the wildcard as being more efficient for the task than using regexp.
1

Here is one way to do it

SELECT model_name 
FROM models_table 
WHERE LEFT(model_name,LEN(model_name)-3)='new_model_01'

Comments

0

You need to use a regular expression for that. In Postgres this can be done using ~ or similar to:

SELECT model_name 
FROM models_table 
WHERE model_name ~ 'new_model_01[0-9]{3}'

1 Comment

I just saw somewhere around that its better to avoid usage of similar to
0

The curly brackets represents length of the input. Can use regex_matches(..) to match a regex

   SELECT model_name FROM 
    models_table WHERE 
   regexp_matches(model_name,  
   'new_model_01[0-9]{3}%') is not null

4 Comments

what does {10} mean?
curly brackets indicates no of digits or length
LIKE does not support regular expressions
umm.. never thought of like wouldnt support regular expressions as it supports wild card, patterns via %

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.