MYSQL Timestampdiff output formatting last two decimals - mysql

Simple question
What is the best way to change the output so that it only displays the last two decimals?
SELECT AVG(TIMESTAMPDIFF(hour, start_it,start_qa)
So that the output of '13.2500' displays as '13.25'.
Thanks for your help.

Normally, you would do this in the application layer. If you insist on doing it in the database, you can use format():
SELECT FORMAT(AVG(TIMESTAMPDIFF(hour, start_it,start_qa), 2)

Related

Sum columns with similar names in SQL

Relatively new to SQL and want to shorten a query I’m using.
The goal is to add the total spent in one year and compare it to the next year. However, the column names are all formatted “Spend_YYYYMM” so “Spend_202102.”
Currently, my solution is just to add all 12 columns up:
SELECT
“Full_Name”,
(“Spend_202001”+”Spend_202002”...) AS “2020 Total”,
(“Spend_201901”+”Spend_201902”...) AS “2019 Total”
FROM “Customers”
WHERE “2019 Total” > “2020 Total”;
So is there a way to look for columns where it starts with “Spend_2019” and add them up without having to type all 12 columns out? Or is what I have the only way we can really do this?
(Sorry for all the superfluous quotes, it’s apparently how our DB works with SQL.)
Thank you for your help!!
First, do not use identifiers that need to be escaped.
Second, your data model is weak. You should have separate rows for the different years.
But, the answer to your question is a MySQL extension of the HAVING clause:
SELECT Full_Name,
(Spend_202001 + Spend_202002 ...) AS Total_2020,
(Spend_201901 + Spend_201902 ...) AS Total_2019
FROM Customers
HAVING Total_2019 > Total_2020 ;

How to find a record which best matches to given string using only mySQL database?

I am having a hard time figuring this one out so I came here for help.
I have a table x_25_operators which has a field called mask.
Masks are numbers with length between 7-11 digits. What I am trying to achieve here is to find the best match for my query at this particular table.
Given the scenario:
x_25_operators
| some_other_data | mask
. 486737
. 616724
. 915776
I have a number: 48915776148 (this number is fixed-length, always 11 digits)
I am looking for a query which will return a row containing mask 915776 (or all rows fitting to this searched number as filtering best-matching-one out should be a piece of cake).
I was thinking of using LIKE as the filter but such query:
SELECT * FROM x_25_operators WHERE mask LIKE '48915776148'
returns an empty query (which should be kind of obvious).
I am using a mySQL database.
Any ideas how to tackle such problem? I am open to any suggestions.
You can use LOCATE()
SELECT * FROM x_25_operators WHERE LOCATE(mask , "48915776148")
Here is some more info: https://www.w3resource.com/mysql/string-functions/mysql-locate-function.php

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.

MySQL is cutting '00' in decimals

I have a table with a decimal column with a lenght = 9 and decimals = 2.
If I put a value of 21.59 (for example) it works ok.
If I put 52.00 it writes only 52. I need to keep 52.00 instead.
Master question: Can the database store the value this way? Instead of
using format/cast in select to retrieve the value...
As noted bellow, this make sense:
"You shouldn't worry about display formatting issues at the database
level but at the ... display level"
Use the FORMAT function:
select format(mycolumn, 2) from mytable;
This also has the effect of adding thousand's separator into the number, so you would get output like 123,456.70. There are workarounds if this doesn't work for you.
Given that MySQL doesn't have the world's best facilities for formatting numbers, display issues like this are usually handled in client code.

Determine how many characters in text are within another text entry in MySQL

I have a table where there is a column of type 'text'. I know I can compare two entries to see is they are the same using a simple select statement. Is there a way to compare two entries and return how similar they are? More specifically, can it say how many characters are different between the two?
For example, suppose one entry is:
This is a line.
And another that is:
This is a line. And another.
I believe I can write a select statement that says the first in contained in the second. But is there a way to alert me that the first is in the second AND there are 15 extra characters in the 2nd?
Try to use Levinshtein distance http://www.artfulsoftware.com/infotree/queries.php#552
You can use LENGTH along with LIKE to do this. E.g.:
INSERT INTO test VALUES("HELLO WORLD");
select LENGTH(name)-length("HELLO") from test where name like "%HELLO%";
So you'd need to programmatically replace HELLO with whatever the string was you wanted to search for.
Is that what you were looking for?
You could simply measure the length of both as strings with char_length() and subtract the difference?
MySQL: char_length()
(Note that length() and char_length() return different values!)