简体   繁体   中英

SQL - Like and greater than Function

I have a dataset like below:

table_a

Product_Name      Product_Orders
    game_296                   1
    game_298                   2
    game_299                   4
    300_game                   6
    xyz_game                   9
    game-tyw                  12

How do I do use a like function in SQL and combine with a greater than? My overall goal is to filter games that are greater than a certain number like 297.

I would ideally like to do something like this:

select * from table_a
where Product_Name > ilike %297%

The expected output here would be this:

Product_Name      Product_Orders
    game_298                   2
    game_299                   4
    300_game                   6

One way would be to remove all non-digits from the string and then do the comparison:

where cast(regexp_replace(product_name, '[^0-9]', '') as int) > 297

Try this:

-- your input ....
WITH
indata(Product_Name,Product_Orders) AS (
          SELECT 'game_296', 1
UNION ALL SELECT 'game_298', 2
UNION ALL SELECT 'game_299', 4
UNION ALL SELECT '300_game', 6
UNION ALL SELECT 'xyz_game', 9
UNION ALL SELECT 'game-tyw',12
)
-- real query starts here ...
SELECT
  product_name
, product_orders
FROM indata
WHERE CAST(REGEXP_SUBSTR(product_name,'\d+') AS INTEGER) > 297;
-- out  product_name | product_orders 
-- out --------------+----------------
-- out  game_298     |              2
-- out  game_299     |              4
-- out  300_game     |              6
-- out (3 rows)

Maybe like this:

select * from table_a
where Product_Name like %[2][9][8-9]% or Product_Name like %[3-9][0-9][0-9]%

I don't know the SQL you use, it may be wrong as a syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM