First order by uppercase & lowercase - mysql

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

Related

MYSQL - Order column by ASCII Value

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

Order a VARCHAR column numerically

Current SQL: SELECT code FROM myTable ORDER BY code ASC
code
---
11
113
12
13A
This is the current order I have of a MySQL table.
I want the order to be A-Z, 1-10 however, numerically, like this:
code
---
11
12
13A
113
The reason I cannot achieve this effect in the first place is because the code column is varchar and not int. However as shown in the example, some codes have a letter prepended to them so I cannot change this to integer.
How can I get around this problem without changing the data type?
The simplest way is to use silent conversion. Just add 0:
order by code + 0
In practice, you might want:
order by code + 0, code
This should work, sorting by number first and alpha after (if the same number):
select * from myTable
order by cast(replace(code,'[0-9]+','') as unsigned), code
See this SQL Fiddle

Mysql get values from column having more than certain characters without punctuation

How can I get all the values from mysql table field, having more than 10 characters without any special characters (space, line breaks, colons, etc.)
Let's say I have table name myTable and the field I want to get values from is myColumn.
myColumn
--------
1234
------
123 456
------
123:456
-------
1234
5678
--------
123-456
----------------
1234567890123
So here I would like to get all the field values except first one i.e. 1234
Any help is much appreciated.
Thanks
UPDATE:
Sorry if I was unable to give proper description of my problem. I have tried it again:
If there is count of more than 10 characters without punctuation, then retrieve that as well.
Retrieve all the values which have special characters like line break, spaces, etc.
Yes, I have primary key in this table if this helps.
The logic seems to be "more than 10 characters OR has special punctuation":
where length(mycol) > 10 or
mycol regexp '[^a-zA-Z0-9]'
SELECT MyColumn
From MyTable
WHERE MyColumn RLIKE '([a-z0-9].*){10}'
[a-z0-9] matches a normal character.
([a-z0-9].*) matches a normal character followed by anything.
{10} matches the preceding regexp 10 times.
The result is that this matches 10 normal characters with anything between them.

Need a help for sort in mysql

Hi I want to sort a table .The field contains numbers,alphabets and numbers with alphabets ie,
1
2
1a
11a
a
6a
b
I want to sort this to,
1
1a
2
6a
11a
a
b
My code is, SELECT * FROM t ORDER BY CAST(st AS SIGNED), st
But the result is,
a
b
1
1a
2
6a
11a
I found this code in this url "http://www.mpopp.net/2006/06/sorting-of-numeric-values-mixed-with-alphanumeric-values/"
Anyone please help me
This would do your required sort order, even in the presence of 0 in the table;
SELECT * FROM t
ORDER BY
st REGEXP '^[[:alpha:]].*',
st+0,
st
An SQLfiddle to test with.
As a first sort criteria, it sorts anything that starts with a letter after anything that doesn't. That's what the regexp does.
As a second sort criteria it sorts by the numerical value the string starts with (st+0 adds 0 to the numerical part the string starts with and returns an int)
As a last resort, it sorts by the string itself to get the alphabetical ones in order.
You can use this:
SELECT *
FROM t
ORDER BY
st+0=0, st+0, st
Using st+0 the varchar column will be casted to int. Ordering by st+0=0 will put alphanumeric rows at the bottom (st+0=0 will be 1 if the string starts with an alphanumeric character, oterwise it will be 0)
Please see fiddle here.
The reason that you are getting this output is that all the character like 'a', 'b' etc are converted to '0' and if you use order by ASC it will appear at the top.
SELECT CAST(number AS SIGNED) from tbl
is returning
1
2
1
11
0
6
0
Look at this fiddle:- SQL FIDDLE
I did small change in your query -
SELECT *, CAST(st AS SIGNED) as casted_column
FROM t
ORDER BY casted_column ASC, st ASC
this should work.
in theory your syntax should work but not sure why mysql doesn't accept these methods after from tag.
so created temp field and then sorted that one .
This should work as per my experience, and you can check it.
SQL FIDDLE

mysql sort varchar with numbers

How can I sort this data numerically rather than lexicographically?
100_10A
100_10B
100_10C
100_11A
100_11B
100_11C
100_12A
100_12B
100_12C
100_13A
100_13B
100_13C
100_14A
100_14B
100_14C
100_15A
100_15B
100_15C
100_16A
100_16B
100_16C
100_1A
100_1B
100_1C
100_2A
100_2B
100_2C
100_3A
100_3B
100_3C
100_4A
100_4B
100_4C
100_5A
100_5B
100_5C
100_6A
100_6B
100_6C
100_7A
100_7B
100_7C
100_8A
100_8B
100_8C
100_9A
100_9B
100_9C
select generalcolum from mytable order by blockid, plotid ASC
What I need out of this sort order is
100_1A
100_1B
100_1C...
...
...
100_10A
100_10B
100_10C
What I need to do in some way is have a zero added before the sort happens so that, I can get them in the order I want.
There are two colums, one that stores the 100 (number before the underscore) and one that stores the 1A the value after the underscore.
My sudo crap select
select thiscolum this table
order by blockid, plotid(+1 zero to prefix if len(plotid) < 2)
For example if the plot value is 1A, to do the best sorting, i need it to be looked at as 01A so that it comes before 10A.
order by length(blockid), blockid, length(plotid), plotid