1

In a column I have something like this:

Amount:

12
2x25
192

How is it possible to multiply in this example 2x25 to order it correctly ASC.

My starting point:

SELECT * FROM table
ORDER BY REPLACE(Amount,'x','*') ASC

TIA frgtv10

1

2 Answers 2

4

try this

  SELECT 
  CAST(if(Amount LIKE '%x%', SUBSTRING_INDEX(Amount, 'x', 1) *
  SUBSTRING_INDEX(Amount, 'x', -1) , Amount) as  unsigned ) as amount 
  FROM table1
  ORDER BY Amount ASC

DEMO HERE

steps and explaining :

  • locate fields with x value

  • sbstring from left and right and multiply it.

  • then cast the multiplication as unsigned.

  • order it asc

Sign up to request clarification or add additional context in comments.

1 Comment

makes my answer redundant, and ruins my attempt at education, +1 :-)
1

As long as this is the only formula (multiplying 2 numbers), you should be able to hard-code it with INSTR, SUBSTRING, and CONVERT.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.