0

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 

3 Answers 3

2

You can try it-

ORDER BY CAST(RIGHT(SUBSTRING_INDEX(title,'%',1),2) AS SIGNED) DESC 
Sign up to request clarification or add additional context in comments.

Comments

0

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 

1 Comment

Colum pattern can be diffrent. it is not in fixed patter. it can also like :- 50% off, 50% flat discount etc. @mukesh
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.