how to sort varchar numeric columns? [duplicate] - mysql

This question already has answers here:
Natural Sort in MySQL
(22 answers)
Closed 8 years ago.
I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like
I want to sorting numeric value but its datatype is varchar then how
value like this
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
but i want to in sequence last
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
and also I have string like this
1.2.840.114257.0.10325113632288210457800001002296133400001
1.2.840.114257.0.10379710976288210457800000002296491200000
1.2.840.114257.0.10328923264288210457800000002296158400001
I also want to sort this ...

For your specific example, you can do:
ORDER BY length(col), col
If you have other examples, you might need to "parse" the values using substring_index().

I have tried the following and it is working fine.
First Remove the "." From the String so you have only numeric and then sort by using ABC() Function so, It will not truncated.
SELECT yourcol AS v FROM test ORDER BY ABS(REPLACE(yourcol, '.', '')), yourcol;

I think you should redesign your schema,
If you have only digits in ur input than go for #Gordon Linoff answer
Or may your input also contain character string(eg: ankit123) and you want sort in order to that char and number also than you may go for this
You have to implementing any Algorithm like this
Lets take eg
1 >1.2.1
2 >1.2.10
3 >1.2.2
Now make all digits equal, Decide any max length(eg: 5) so attach 0 ahead to make all digits with same length
now above string look like
1 >00001.00002.00001
2 >00001.00002.00010
3 >00001.00002.00002
Now if you you fire ORDER BY on this string you get your output in sorted order.

Related

MySQL - How to select data by Character range (between min to max)

This question is regarding mysql. I want to get the only characters value of min and max range. I think this is explained better with an example.
Example- The column of table is username like abc123 output should be 3,12de:-o/p->2,erogan44e :o/p->7 etc.
i want the select only characters length between min to max.
Is it possible without using procedure ?
Should we use regular expression ?
To count the number of alphabets in a String, you first need to replace all the non-alphabet characters with an empty space and then calculate the length, e.g.:
SELECT LENGTH(REGEXP_REPLACE(column_name, '^[A-Za-z]', '')) AS value
FROM table
ORDER BY value DESC;
The problem with this approach is, MySQL does not have REGEXP_REPLACE function (unlike MariaDB). So, you will have to write your own function for this, have a look at this example.

mysqli - I am trying to get substring working

SELECT update_log, update_idccode, update_filenumber, update_filetype, update_timedate
FROM updates_log
WHERE substring(update_idccode,0,7) ='idc2997%' AND update_filetype = 'E'
ORDER BY update_log DESC
I am trying to get this to get the first 7 characters of my update_idccode table column. I cannot get it to work. Any thoughts?
You're pulling out 7 chars from your column, and comparing them against an 8-char string. In other words, it is imposssible for a 7-char word to ever be identical to an 8-char word.
Try
WHERE substring(update_idccode,0,7) = 'idc2997' ...
instead. As an alternative,
WHERE update_idccode LIKE 'idc2997%' ...
would also work. The % suggests you may have been trying this, but % is only relevant as a wildcard in LIKE comparisons, not = equality testing.

mysql min value column of floating numbers

Is it possible to find the min value of a column of floating numbers using a mysql function? Suppose I have the following table:
id | value a | 24.88 a | 119.99
If I try:
SELECT MIN(value) FROM [table name] GROUP BY id;
mysql returns:
119.99
After testing this with different floating numbers I believe that this is the case because mysql takes the first character in each of the strings "1" and "2" and then selects a min based on which character is smaller.
I've read through this forum and others trying to find an answer but it seems nobody has raised this problem.
I should mention I've also tried CEIL(value) but that function also seems to have some bugs and I'd prefer to keep the number a floating number and not an integer.
Thanks everyone.
It looks like the column is being stored as a character-based data type. You can solve this in one of two ways:
Change the column type to a numeric type
change the query to add CAST around the value: MIN(CAST(value AS DECIMAL))
The column change might look like this:
ALTER TABLE my_table MODIFY COLUMN value double;
And, as far as I know, MySQL will attempt to convert the data for you. See the note here, which states it "tries".

Finding number of occurence of a specific string in MYSQL

Consider the string "55,33,255,66,55"
I am finding ways to count number of occurence of a specific characters ("55" in this case) in this string using mysql select query.
Currently i am using the below logic to count
select CAST((LENGTH("55,33,255,66,55") - LENGTH(REPLACE("55,33,255,66,55", "55", ""))) / LENGTH("55") AS UNSIGNED)
But the issue with this one is, it counts all occurence of 55 and the result is = 3,
but the desired output is = 2.
Is there any way i can make this work correct? please suggest.
NOTE : "55" is the input we are giving and consider the value "55,33,255,66,55" is from a database field.
Regards,
Balan
You want to match on ',55,', but there's the first and last position to worry about. You can use the trick of adding commas to the frot and back of the input to get around that:
select LENGTH('55,33,255,66,55') + 2 -
LENGTH(REPLACE(CONCAT(',', '55,33,255,66,55', ','), ',55,', 'xxx'))
Returns 2
I've used CONCAT to pre- and post-pend the commas (rather than adding a literal into the text) because I assume you'll be using this on a column not a literal.
Note also these improvements:
Removal of the cast - it is already numeric
By replacing with a string one less in length (ie ',55,' length 4 to 'xxx' length 3), the result doesn't need to be divided - it's already the correct result
2 is added to the length because of the two commas added front and back (no need to use CONCAT to calculate the pre-replace length)
Try this:
select CAST((LENGTH("55,33,255,66,55") + 2 - LENGTH(REPLACE(concat(",","55,33,255,66,55",","), ",55,", ",,"))) / LENGTH("55") AS UNSIGNED)
I would do an sub select in this sub select I would replace every 255 with some other unique signs and them count the new signs and the standing 55's.
If(row = '255') then '1337'
for example.

How to get last 12 digits from a string in MySQL?

How would I get last 12 digits of a string using mysql?
Let's say I have a varchar field with a tracking number, that may be anywhere from 5 to 20 varchars long. But I only need to select last 12 digits or less if there are less.
so in a field = 12345678123456789012
I would only need to get what's in brackets
field = 12345678[123456789012]
I saw a few examples using mid, etc, but they dont' produce the desired result or I can't find an example that makes sense :-(
Thank you.
SELECT RIGHT(field, 12);
Nick,
Try using the RIGHT(str, len) function.
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right
I'm not sure of the semantics if the string is shorter than length as I don't have access to MySQL but it might do what you're looking for.