MYSQL - Order column by ASCII Value - mysql

I have a column with texts, sorted by ASCII it should be ordered as:
- (hyphen)
0
1 (numbers)
2
A (uppercase)
B
_ (underscore)
a
b (lowercase)
c
However it is being ordered as:
- (hyphen)
0
1 (numbers)
2
a
b (lowercase)
c
A
B (uppercase)
C
_ (underscore)
How can I do the sorting by ASCII value?

The sort order is controlled by the collation. You can use the BINARY collation to sort by raw bytes, which in the case of ASCII data will cause it to sort by ASCII value. See https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
SELECT ...
FROM mytable
ORDER BY BINARY mycolumn
This will be more flexible than using the ASCII() function because that function only returns the ASCII value of the first character. Using the BINARY collation allows sorting by the full string.

You could use ASCII:
SELECT *
FROM tab
ORDER BY ASCII(col_name) ASC

Related

First order by uppercase & lowercase

I have a few records which start with lowercase & uppercase.
SELECT *
FROM wording
ORDER BY word ASC;
Lanza
Mensi
Mhiob
blackbery
umbre
apple
Etios
Iomio
I am trying to order this by A-Z and a-z, something like
```none
Etios
Iomio
Lanza
Mensi
Mhiob
apple
blackberry
umbre
So, all the word which is starting with capital come first and after then all lowercase words.
I think ASCII function might help you -
SELECT * FROM wording
ORDER BY CASE WHEN ASCII(word) BETWEEN 65 AND 90 THEN 1 ELSE 2 END ASC, word DESC;
This should work too:
select *
from wording
order by binary(word) ASC;
Result:
word
Etios
Iomio
Lanza
Mensi
Mhiob
apple
blackbery
umbre
Demo
The binary sorting would put capital letters before lowercase ones
you can use ASCII Method to get desired result. It will sort Value on the basis of Numeric ASCII Value of Character it means if we Sort data ascending it will sort Capital Letter First. ASCII of (A-Z: 65-90) and ASCII of (a-z: 97-122)
SELECT * FROM wording ORDER BY ASCII(word) ASC;
Output:
Etios
Iomio
Lanza
Mensi
Mhiob
apple
blackbery
umbre
Reference Website:SQL Practice

How to create arabic character class in MySQL using RegExp?

For example I want to create a class character, ara digit [٩-٠] .. content all digits.
The corresponding Unicode is [U+0660-U+0669], I tried this:
Select * FROM employees WHERE ID REGEXP [\u{0660}-\u{0669}];
I get this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[\u{0660}-\u{0669}] LIMIT 0, 25' at line 1"
https://dev.mysql.com/doc/refman/5.7/en/regexp.html says
Warning
The REGEXP and RLIKE operators work in byte-wise fashion, so they are
not multibyte safe and may produce unexpected results with multibyte
character sets. In addition, these operators compare characters by
their byte values and accented characters may not compare as equal
even if a given collation treats them as equal.
That is, if you use à in a regexp, it will treat the 2-byte utf8 code as 2 bytes (in hex) C3 and 83. If this gives you the 'right' answer, it will be more by 'luck' than design.
This does work:
mysql> SELECT '١' REGEXP '[٩-٠]';
+-----------------------+
| '١' REGEXP '[٩-٠]' |
+-----------------------+
| 1 |
+-----------------------+
But, it is just coincidence. The regexp is something like [x0-x9] where x is the D9 byte, 0 is A0 and 9 is A9. But then the regexp is "any character x, or between 0 and x, or 9, which is not what you wanted.
This might work for 'all' of Arabic: REGEXP UNHEX('5BD82DDD5D'), but only because 'all' start with hex D8 through DD. (However, there may be other things in that range.) Furthermore, that will only check "Does the string contain an Arabic letter; it cannot used for anything more complex, such as phrases or a subset of letters.
Back to the digit range. Just checking for hex D9 is not safe, because that will include percent sign, superscript letters, and other characters. This may work: REGEXP UNHEX('D95BA02DA95D').
Caveat: Most of what I have said in this answer is untested; I'm inventing a solution in an area where I have no experience (REGEXP with utf8).

mysql where string ends with numbers

My table column contains values like:
id | item
-------------
1 | aaaa11a112
2 | aa1112aa2a
3 | aa11aa1a11
4 | aaa2a222aa
I want to select only rows where value of item ends with numbers.
Is there something like this?
select * from table where item like '%number'
You can use REGEXP and character class
select * from table where item REGEXP '[[:digit:]]$'
DEMO
Explanation:
[[:digit:]] >> Match digit characters
$ >> Match at the end of the string
Within a bracket expression (written using [ and ]), [:character_class:] represents a character class that matches all characters belonging to that class.
SIDENOTE:
Other helpful mysql character classes to use with REGEXP, taken from the documentation:
Character Class Name Meaning
alnum Alphanumeric characters
alpha Alphabetic characters
blank Whitespace characters
cntrl Control characters
digit Digit characters
graph Graphic characters
lower Lowercase alphabetic characters
print Graphic or space characters
punct Punctuation characters
space Space, tab, newline, and carriage return
upper Uppercase alphabetic characters
xdigit Hexadecimal digit characters
you can use REGEXP
select * from table where RIGHT(item ,1) REGEXP '^-?[0-9]+$';
Yes you can use like for numbers.
select * from table where item like '%1'
This will work

substring dynamic number of characters

I'm working with 2 sets of data that were merged together, but they're inconsistent in their format. Some are 10 characters, all numbers. Others may have a separator : at position 4. I need to substring the first 4 characters. But if the 4th character is a : substring only the first 3 characters.
Does mysql have an IF functionality to determine the number of characters to substring based on the character in position 4?
select substring(id, 1 , 3/4) from table1
You can treat the field like it's colon separated and do this to select only the first part:
SELECT SUBSTRING_INDEX(id, ':', 1)
See also: SUBSTRING_INDEX()

mysql sort order, need slightly different result

I have a varchar column that I am currently sorting by using: ORDER BY (col_name+0)
This column contains both digits and non-digits, and the result of this sorting is this:
D3
D111
M123-M124
M136
4
9
10
25
37b
132
147-149
168b
168ca
This sorting is almost perfect for our application, but with one exception: we want the items that start with letters to display after those that start with numbers. This being the ideal result:
4
9
10
25
37b
132
147-149
168b
168ca
D3
D111
M123-M124
M136
I'm hoping this can be achieved in the select statement, rather than needing to loop through everything in code again after the select. Any ideas?
You can use this:
ORDER BY
col_name regexp "^[^0-9]",
case when col_name regexp "^[0-9]" then col_name + 0
else mid(col_name, 2, length(col_name )-1) + 0 end,
col_name
this will put rows that begins with a digit at the top. If col_name begins with a digit, I'm sorting by it's numeric value, if not I'm sorting by the numeric value of the string beginning at the second character.