简体   繁体   中英

Sql query to select data in asc desc order with find number in string

How can I find the number from string with a SQL query so I select data in asc and desc order with this number?

I have table bf_offers and a column title with values like this:

title
---------------
Flat 20% off
Flat 50% off
Upto 40% off

I want to find the number like 20, 50 & 40 and arrange title with in desc or asc order. If I set to desc order then data should be ordered like this:

title
-----------------
Flat 50% off
Flat 40% off
Upto 20% off

你可以试试-

ORDER BY CAST(RIGHT(SUBSTRING_INDEX(title,'%',1),2) AS SIGNED) DESC

Using substring function do this.

order by SUBSTRING(title, CHAR_LENGTH(title) - 5,2) 

or descending

order by SUBSTRING(title, CHAR_LENGTH(title) - 5,2) desc

Try following,

SELECT CAST((SELECT SUBSTRING(title, n, 1)
              FROM tablename
              WHERE n <= LEN(title)
                AND SUBSTRING(titale, n, 1) LIKE '[0-9]'
              FOR XML PATH('')) AS float)
FROM tablename

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