Sql order string with numbers asc - mysql

I have strings in my db like this:
DE-1016-860
DE-1016-1078
DE-1016-1166
How can I ORDER BY order_numbers this elements in a SELECT like this:
DE-1016-1166
DE-1016-1078
DE-1016-860

Found a solution:
ORDER BY SUBSTR(order_number FROM 1 FOR 8), CAST(SUBSTR(order_number FROM 8) AS UNSIGNED)
First I gave them the value 1 for the startpoint and 8 for the endpoint:
12345678
DE-1016-
You can see the eight characters. Second I make a cast from the eighth number and it works fine. It give me my numbers sorted by by the highest at the top to the smallest at the end.

If your format is always AA-####-.........
then you can try using LEFT(), RIGHT(), and SUBSTRING().
For example:
ORDER BY LEFT(order_number,2), SUBSTRING(order_number,4,4) DESC, SUBSTRING(order_number, 9,4) DESC

This assumes your prefix is always the same, thus to_number the last characters:
SELECT str
FROM (SELECT 'DE-1016-860' str FROM DUAL
UNION ALL
SELECT 'DE-1016-1078' str FROM DUAL
UNION ALL
SELECT 'DE-1016-1166' str FROM DUAL)
ORDER BY TO_NUMBER (SUBSTR (str, 9, 4)) DESC

Related

sql extract only numeric value from column

I have a column called' memo_line_2',the value format is like :'$3000.00 (card limit increase)',how can I only extract numeric value from the column?Thanks
example:
'$3000.00 (card limit increase)' -> 3000
'$5000.00 (card limit increase)' -> 5000
'$12000.00 (card limit increase)' ->12000
You could use REGEXP_SUBSTR() for this:
SELECT REGEXP_SUBSTR(tmp.`value`, '[0-9]+') as `new_value`
FROM (SELECT '$3000.00' as `value` UNION ALL
SELECT '$5000.00' as `value` UNION ALL
SELECT '$12000.00' as `value`) tmp
Returns:
new_value
---------
3000
5000
12000
If you would like to keep everything after the decimal, use '[0-9.]+' as your regular expression filter.
If your data will be always in this format you can use below query to select the data between $ and . :
SELECT substring_index(substring_index(memo_line_2, '$', -1), '.', 1)
FROM your_table;
Refrence: MySQL substring between two strings
You can use:
select regexp_substr(col, '[0-9]+[.]?[0-9]*')
This will extract the digits with the cents. You can then convert to an integer or numeric:
select cast(regexp_substr(col, '[0-9]+[.]?[0-9]*') as unsigned)
It can be done by making a custom function in your sql query
Take a look at:
https://stackoverflow.com/a/37269038/16536522

How to natural sort “X-Y” string data, first by X and then by Y?

Given this data:
W18-40461
W19-1040
W20-4617
W20-100
I've tried several of the common natural sorting methods for mysql, but they won't sort these in a natural descending way, like:
W20-4617
W20-100
W19-1040
W18-40461
For example:
select theID
from Table
where theID
order by lpad(theID, 9, 0) desc
Assuming the parts on either side of the - are limited to 2 digits and 5 digits respectively, you can extract the two numeric values using SUBSTR (and LOCATE to find the - between the two numbers) and then LPAD to pad each of those values out to 2 and 5 digits to allow them to be sorted numerically:
SELECT *
FROM data
ORDER BY LPAD(SUBSTR(id, 2, LOCATE('-', id) - 2), 2, '0') DESC,
LPAD(SUBSTR(id, LOCATE('-', id) + 1), 5, '0') DESC
Output (for my expanded sample):
id
W20-12457
W20-4617
W20-100
W19-1040
W18-40461
W4-2017
Demo on db-fiddle
If the values can have more than 2 or 5 digits respectively, just change the second parameters to LPAD to suit.
I would do this as:
order by substring_index(col, '-', 1) desc,
substring_index(col, '-', -1) + 0 desc
This orders by the part before the hyphen as a string. And it converts the part after the hyphen to a number for sorting purposes.

MySQL and PHP using rank by ASC

Trying to fix ranking off scores table, I put this in;
SELECT * from scores where tournament_id = "3" AND class = "MA1" ORDER BY Rank ASC
It came out
1
11
12
13
2
22
3
31
4
5
6 and on, how can it be fixed?
Thanks
Your rank column data type is string. So it is returning as above please convert it in number or int format.
SELECT CAST("10" as SIGNED) ;
Seems like your rank column is a textual column (e.g., varchar), and therefor, when you order by it, you order lexicographically. If you want to sort it numerically, you need to explicitly convert it:
SELECT *
FROM scores
WHERE tournament_id = "3" AND class = "MA1"
ORDER BY CAST(rank AS INTEGER) ASC
-- Here -^
SELECT * from scores
where tournament_id = "3"
AND class = "MA1"
ORDER BY CAST(Rank AS INTEGER) ASC
You should casting the type char to int your rank field for order by

mysql select in aescending or descending order

my id or primary key is and the data type of is VARCHAR(50)
0.0.01
0.0.100
0.0.101
0.0.1011
0.0.201
0.0.501
0.0.99
0.0.999
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
100.100.100
25.45.1001
99.99.99
I have to get it in sorted order
so i tried this
select id from table order by cast(id as decimal) desc;
but it does not work
the expected order is after running the query
0.0.01
0.0.99
0.0.100
0.0.101
0.0.201
0.0.501
0.0.999
0.0.1011
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
25.45.1001
99.99.99
100.100.100
i am using mysql for this
Not an easier one but you can use substring_index for each decimal places
select *
from t
order by
substring_index(id,'.',1) * 1,
substring_index(substring_index(id,'.',-2),'.',1) * 1,
substring_index(id,'.',-1) * 1
Explanation
I have use substring_index what it does it will return the piece of string in provided column like in above case i have use id column by the occurrence of delimiter i.e(.) for example a string like 0.1.2 for above 3 sunstring_index usage will return as below
substring_index('0.1.2','.',1) will give result as 0
substring_index(substring_index('0.1.2','.',-2),'.',1) will give result as 1
substring_index('0.1.2','.',-1) will give result as 2
For type casting to number i have multiplied the result of substring_index to 1 so the order by expression will first order the results by the number before first dot then with number before second dot and last the number after second dot in ascending manner
Demo
Sources:
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php
Your ID column has invalid decimal value so casting could not work here.
Try without casting this should work:
select id from table order by id desc;
DEMO

how to sort varchar numeric columns by DESC or ASC?

I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like 1, 10, 2, instead of 1, 2, 10.
How can I do it to sort like 1, 2, 10?
order by
cast(column as float)
Notes:
Assumed you only have numbers in the columns. No "fish" or "bicycle"
empty strings CAST to zero
Edit: For MySQL. You can not cast to float
order by
cast(column as decimal(38,10))
You can cast to int...
order by cast(column as int)
DEMO
DECLARE #q as table(
name varchar(50),
columnn varchar(10)
)
insert into #q
VALUES('one','1'),('one','10'),('one','20'),('one','3'),('one','2'),('one','20')
select * from #q order by cast (columnn as int) desc
prints
-------------------------------------------------- ----------
one 20
one 20
one 10
one 3
one 2
one 1
So, Daniel, yes, it works :)
UPDATE:
order by cast(column as decimal(20,6))
Will cast the column values to decimal numbers with 20 digits max and 6 decimal places. Adjust it to your actual requirements.
Try this:
order by CAST(column as UNSIGNED)
i used this way
multiply it with one the query is :
ORDER BY columnname * 1 ASC
Example: Table user have value with column value [varchar(20)].
Then you can query it:
SELECT * FROM user ORDER BY value * 1
After we multiply it MySQL will treat it like a number but this way is not recommended for a heavy load.