Choosing Values ​​Using \ d

I have a table with the values:

--Product--
ASUS22
ASUSI522
ASUSI7256
ASUSI2262
ASUSI1267
ASUSI764
ASUSI712

etc. I am trying to select products with it, starting with ASUSI and only 3 integers after it.

Someone said that I can use \ d \ d \ d for this, but it does not work (below)

select product from products where product like '%ASUS\d\d\d%'

So, I want to select the values:

ASUSI712
ASUSI764
ASUSI522

How can I do that? Thanks, Regards,

+4
source share
4 answers

To use character classes, you need to use SIMILAR TOinstead LIKE:

select product from products where product similar to '%ASUS\d\d\d'

As @ lad2025 notes, your original request does not meet your expectation, so you need to remove the final %one in order to limit the match of three numbers.

+1
source

, ASUSI :

^ASUSI\d{3}$

^ASUSI  - starts with 'ASUSI'
\d{3}   - followed by exactly three digits
$       - followed by the end of the string

SELECT product
FROM products
WHERE product ~ '^ASUSI\d{3}d$'
+1

There may be a more direct way, but you can use length()to achieve this:

SELECT product
FROM   products
WHERE  product like 'ASUSI%'
AND    length(product) = 8
AND    product ~ '\d$'
0
source

How about this? using LIKE ASUSI and using 3 characters checking the range from 1 to 9.

DECLARE @test TABLE(Product VARCHAR(15))
INSERT INTO @test(Product)
VALUES('ASUS22'),
('ASUSI522'),
('ASUSI7256'),
('ASUSI2262'),
('ASUSI1267'),
('ASUSI764'),
('ASUSI712'),
('ASUSI72K'),
('ASUSI7234')

SELECT *
FROM @test

SELECT *
FROM @test
WHERE Product LIKE 'ASUSI[1-9][1-9][1-9]'
-1
source

All Articles