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

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.

Related

MySQL REPLACE string with regex

I have a table with about 50,000 records. One of the fields is a "imploaded" field consisting of variable number of parameters from 1 to 800. I need to replace all parameters to 0.
Example:
1 parameter 3.45 should become 0.00
2 parameters 2.27^11.03 should become 0.00^0.00
3 parameters 809.11^0.12^3334.25 should become 0.00^0.00^0.00
and so on.
Really I need to replace anything between ^ with 0.00 ( for 1 parameter it should be just 0.00 without ^).
Or I need somehow count number of ^, generate string like 0.00^0.00^0.00 ... and replace it. The only tool available is MySqlWorkbench.
I would appreciate any help.
There is no regex replace capability built in to MySQL.
You can, however, accomplish your purpose by doing what you suggested -- counting the number of ^ and crafting a string of replacement values, with this:
TRIM(TRAILING '^' FROM REPEAT('0.00^',(LENGTH(column) - LENGTH(REPLACE(column,'^','')) + 1)));
From inside to outside, we calculate the number of values by counting the number of delimiters, and adding 1 to that count. We count the delimiters by comparing the length of the original string, against the length of the same string with the delimiters stripped out using REPLACE(...,'^','') to replace every ^ with nothing.
The REPEAT() function builds a string by repeating a string expression n number of times.
This results in a spurious ^ at the end of the string, which we remove easily enough with TRIM(TRAILING '^' FROM ...).
SELECT t1.*, ... the expression above ... FROM table_name t1, from your table to verify the results of this logic (replacing column with the actual name of the column), then you can UPDATE table SET column = ... to modify the values. once you are confident in the logic.
Note, of course, that this is indicative of a problematic database design. Each column should contain a single atomic value, not a "list" of values, as this question seems to suggest.

Simple select issue with -(dash) in where clause

I need to search for a value like 1234-abc. The database doesn't have this particular value, but has another value 1234. Now the problem is when I write my query like
SELECT * FROM words WHERE tval='1234-abc'
instead of fetching an empty recordset, it fetches the 1234 value, it seems to ignore anything after the -, any idea what's going on?
http://sqlfiddle.com/#!2/9de62/3
You can use the BINARY keyword for the exact match
SELECT tval FROM words WHERE BINARY tval='1223-abc';
Binary is a built-in keyword that after your WHERE clause that forces a comparison for an exact case-sensitive match
Fiddle
The existing expression is implicitly converting the string expression to a number - you need to explicitly convert the number to a character strng, like so:
SELECT tval FROM words WHERE convert(tval,char(20))='1223-1ABCDE';
SQLFiddle here.

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.

Is there any way to modify a column before it is ordered in MySQL?

I have a table with a field value which is a varchar(255). The contents of the field can be quite varied:
$1.20
$2994
$56 + tax (This one can be ignored or truncated to $56 if necessary)
I have a query constructed:
SELECT value FROM unnamed_table ORDER BY value
However, this of course uses ASCII string comparison to order the results and does not use any numerical type of comparison.
Is there a way to truly order by value without changing the field type to DECIMAL or something else? In other words, can the value field be modified ('$' removed, value converted to decimal) on the fly before the results are sorted?
You could sort on an expression made to "parse" the text into decimal
SELECT value FROM unnamed_table ORDER BY expression_returning_decimal(value)
Where your expression uses MySQL functions to extract the number from the string depending on what form you expect it to take (something like CAST(value AS DECIMAL(10,2)), but you'll probably need to deal with the extraneous non-numeric characters somehow as I'm not sure what the CAST will give if you don't strip them).
Create a second column without the $-sign, sort on that one and use the data of the original column in your application.
In order to create the helper column and sort on it you would need something like this:
SELECT value, CAST(SUBSTR(value, 2) AS UNSIGNED) sort_col
FROM unnamed_table ORDER BY sort_col