SELECT/LOCATE Query Outputting Incorrect Results - mysql

I have a column (kills) with values separated by colons as such: 0;2;0. I wanted to select whatever the middle digit may be, and came up with this query:
SELECT RIGHT(kills, LOCATE(';', kills) - 1)-LEFT(kills, LOCATE(';', kills) - 1) FROM stats WHERE id='135' AND LOCATE(';', kills) > 0;
(I'm aware the where clause is specific, it was for debug purposes. It would realistically be set to a variable which outputs the row id)
It works perfectly fine when the results all have the same decimal place such as 1;2;3 or 10;20;30, but returns wild results when the case is otherwise, such as 1;20;30 or 10;20;3.
1;20;21 outputs 0. -- 10;20;2 outputs -10.
2;20;21 outputs -1. -- 20;20;2 outputs -20.
9;20;21 outputs -8. -- 90;20;2 outputs -90.
I would like to select the middle value of all of these values even if one value doesn't share the same decimal place.

If you want the middle number of three, then you want the second number. Use substring_index():
select substring_index(substring_index(kills, ';', 2), ';', -1)

Related

how to randomize column data and mask data using mysql

DISCLAIMER: NONE OF THESE VALUES ARE TRUE/REAL, ITS JUST A PRACTICE ASSIGNMENT
how to randomize the last 6 digits of the DBS account and obscuring the first 4 digits of the NRIC number with x using mysql. All values were keyed in manually and do not relate to each other.
Current
Desired Result
Just use substring operations:
SELECT
CONCAT('XXXX', SUBSTRING(NRIC, 5, 4)) AS NRIC,
Name,
Contact,
Salary,
CONCAT(SUBSTRING(DBS_Account, 1, 3), '-XXXXX-X') AS DBS_Account
FROM yourTable;
mysql rand function - https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round
using substring and concat
SELECT CONCAT(SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1))
will give something like 40033-8
then concat with substring
set #a ='038-12645-6';
SELECT CONCAT(substring_index(#a,'-',1),'-',SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1));
'038-63475-5'

MS SQL Query to remove prefixes

I have below like column values and would like to exclude the characters as well as the hyphen and only return digits. The replace function is not entirely helpful as sometimes the character length is 3 and sometimes its 4, see below as the digit length changes as well.
abc-1234567
sdfr-9876540
try-12345678
case-098765
If you want the part after the last hyphen, you can use substring_index():
select substring_index(col, '-', -1)
You can also extract the digits at the end using regexp_substr():
select regexp_substr(col, '[0-9]+$')

count of comma separated values in column mysql

SELECT ifnull(LENGTH(GROUP_CONCAT(varchar SEPARATOR ',')) - LENGTH(REPLACE(GROUP_CONCAT(varchar SEPARATOR ','), ',', '')) + 1,0) AS total
FROM tablename
WHERE date(date and time)=curdate()
varchar this column contain comma separated values with this I am getting count of comma separated values in column exactly but not working for large data.
is there any solution to get count without length problem
You may be hitting limit of group_concat_max_len - that means that anything longer than (by default) 1024 chars will be truncated so you don't get all results.
Possible solution is counting items per row and summing rows counts.
SELECT SUM(ifnull(LENGTH(varchar) - LENGTH(REPLACE(varchar, ',', '')) + 1,0)) AS total
FROM tablename
WHERE date(date and time)=curdate()
That way you don't need to think about that limit at all.
Just raising group_concat_max_len is not really a viable solution because you will only hit it later.
The real solution is to get rid of comma separated lists and introduce another table where each item will be separate row. That way most counts and similar operations are simpler.

select distinct substring values of a field and count the number of instances of a char in that selection

I'm trying to select distinct substring values of a field and count the number of instances of a char in that selection.
I've found this wonderful post which answers half of it.
So, so far, i can count the instances of a char in my field, it works great. Now the even harder part, what if i select a piece of string using :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk
In this case i'm only selecting the last part of the string (everything after the last'-'). How can i apply this formula to chunk (trying to count the number of instances of '_' in the new string ? :
(LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_')
I know i shoud be using HAVING to make operation on chunk as it's not a real field, but how can i do something like :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk, (LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_') AS total FROM my_field HAVING total < 2
The problem here is that i can't use 'chunk' in the last part since it's not a field..
The problem here is that i can't use 'chunk' in the last part since it's not a field..
Replace 'chunk' in the last part with
SUBSTRING_INDEX(my_field, '-', -1)
Don't know what's the problem?

How to order by a part of a column

i have field with content like this: 1, 2, 100 first two numbers can be any size third one can be up to 100.
Now i need to sort my fields by this third number but since i have 2 other numbers i don't know how to do it.
Maybe i could use something like REGEXP or something else?
So far i've tried SUBSTRING but since my two numbers can be any lenght something like
order by SUBSTRING(my_field, 4)
would work but if i have numbers like 32, 451, 30 it takes wrong numbers
Also i use php for to build my query, but i don't think it matters.
You can use SUBSTRING_INDEX. So something like:
SUBSTRING_INDEX(my_field, ',', -1)
EDIT: if you have spaces you might want to do some trimming as well.
ORDER BY SUBSTRING_INDEX(my_field,',',-1)+0 ASC -- or DESC
Use SUBSTRING_INDEX, which grabs the substring up to the nth occurence of the delimiter (in your case, comma), or, in the case of a negative n, it will return the substring after the nth occurence from the end.
To see what I mean try:
SELECT SUBSTRING_INDEX(my_field,',',-1)
FROM my_table
The reason there is a +0 in the ORDER BY is so that mysql sorts these as numbers, not strings.
This should work:
order by CONVERT(SUBSTRING(my_field, LOCATE(",", my_field, RIGHT)), INTEGER)
SELECT *
FROM (
SELECT
SUBSTRING(my_field, 4) AS my_field,
...
FROM
...
WHERE
...
) temp_table
ORDER BY my_field