简体   繁体   中英

how can i select from a mysql table where column value is greater than some value if the column is text and numbers

I have a table called "products" where one column is "SerialNumber". The problem is that this column is text latin1_swedish_ci and values are like "BC00001, BC0002, etc. i need to select all products where serial number is for example greater than some given serial number. i tried selecting like this:

SELECT * FROM 'products' WHERE 'SerialNumber' >= 'BC00563' 

But since this column is text, it' not working. I can't modify the table structure because the this table laready has 20.000 + records. Any help appreciated!

Assuming the value is always two characters followed by numbers, you can strip out the characters and convert the remaining values into number for the comparison, eg:

SELECT * 
FROM `products` 
WHERE `SerialNumber` >= 
    CASE WHEN 'BC00563' regexp '[A-Z]' THEN 
    CONVERT(SUBSTRING("BC00563", 3, LENGTH("BC00563")), UNSIGNED) 
    ELSE 'BC00563' END;

You can use the column name instead of constant value.

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