MySQL: turn decimal into percent within view - mysql

I've inherited a database and am using views in MySQL to return specific information from a database. I'm having a little trouble one portion- turning the returned numeric value that is a decimal into a percentage.
I don't want to do this in the php- I am specifically trying to format everything in the view first. I realize this is not necessarily the way everyone does this, but for my specific situation it is the best overall solution.
The example value in the database: 0.46
What I'd like to have returned: 46%
Relevant portions of current query:
SELECT CONCAT(a.fa_rate, '%') as rate_percent
FROM accounts a;
This returns 0.46%. Is it possible to return the result I want to have without converting the number into a string? The current data type is a float.

try this
SELECT CONCAT(a.fa_rate * 100 , '%') as rate_percent
FROM accounts a;

Related

Access adds extra decimals when grouping in query

Did not found anything to solve this.
I'm doing some easy things on an Access Database 2007 32 bit.
Got this table:
Id_iva Desde Hasta Valor_Iva
2 01/01/2000 31/08/2012 18,00%
4 01/09/2012 31/12/2021 21,00%
5 01/01/2022 31/12/2099 25,00%
Valor_iva is a numeric field, single type. I manually input those numbers, with 2 decimals only (in this case all of them are 0, but it could be something like 18,50% or 20,23% and so on)
If I make a query like this:
SELECT T_IVA.Hasta, T_IVA.Valor_Iva FROM T_IVA;
It works as expected and it returns exactly the values:
But If my query is this:
SELECT T_IVA.Hasta, Sum(T_IVA.Valor_Iva) AS SumaDeValor_Iva FROM T_IVA GROUP BY T_IVA.Hasta;
I get extradecimals in some values.
Can't understand where those decimals come from.
I've googled about CAST and TRUNCATE but I could not apply those (or I don't know how to).
WHAT I WANT: I just want to make a GROUP BY query that does not add those decimals.
Thanks in advance.
If you want exact results, then cast to an exact type before doing any operations. Or, even better, use an exact (non-floating point) type in the first place.
It seems your values fit in the Currency data type. The Decimal data type can be used for larger values with decimals.
SELECT T_IVA.Hasta, Sum(CCur(T_IVA.Valor_Iva)) AS SumaDeValor_Iva FROM T_IVA GROUP BY T_IVA.Hasta;

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

Mysql how to increase serial number

I have stored procedure in MySql which user will pass in a number e.g. 'cr002149'
Now I want to increase the number by 1 for each record in select statment. How can I do that to let the number become e.g. cr002150,cr002151...
Thanks.
You should reconsider your data structure, you might get better luck just using an auto_increment'd integer. Either way, this should do.
CONCAT('cr',
LPAD(
CAST(
SUBSTRING('cr002149',3) AS DECIMAL(0)
)+1,
6,'0'
)
);
The above will return cr002150. First, we get 002149 using SUBSTRING, then cast the string 002149 to an integer and get 2149. Now, we increment this to get 2150, followed by left-padding it by 0s to get 002150, and finally we concatenate cr on the left side.

mysql float data not selecting in where clause

This maybe an easy one but i couldn't get answer.
I need to select float value from table
example table :-
value
10.2
4.5
4.6
4.06
my query
SELECT * FROM table where value = '4.6'
returns empty result set
how can i solve this !
Generally, you should never check equality with floats (unless, potentially, you have the same object). Internally, it is represented with more precision, even if it isn't showing it to you by the time it outputs to the screen. This basic tenet holds true for computing in general.
There are a dozens of schemes for doing this, but here is a simple one, which should make sense:
SELECT * FROM table where value BETWEEN 4.599 AND 4.601
Use decimal instead of float.
A decimal(10,2) will have 2 and only 2 decimal places and can be compared in the same manner as integers.
Especially for monetairy values you should always use decimal, but anywhere where rounding errors are unwanted, decimal is a good choice.
See: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html
Or
MySQL DECIMAL Data Type Characteristics
Today, I also came across the same situation and get resolved just by using FORMAT function of MySQL, It will return the results that exactly match your WHERE clause.
SELECT * FROM yourtable WHERE FORMAT(`col`,2) = FORMAT(value,2)
Explanation:
FORMAT('col name',precision of floating point number)
Hope it helps.
You can also try query
SELECT * FROM table WHERE value LIKE 4.6;
you write:
SELECT * FROM table where round(value, 1) = 4.6

could the MySQL show percent sign (%) in table

i want to show some data in percent.
i have a mathematics formula like:
(qty(S) + qty(B))/qty(id)*100%
could i show the result for example like 25%? how do i do that?
Databases are used for storing data. Presentation of data should not be in its responsibilities. By that, I mean you should very rarely thing about storing a string value in the database like '75%'.
If you want specific formatting, the best place to do it is after extracting the data:
select concat(your_column,'%') as percent ...
Because concat expects strings, numeric values are automagically cast into string before joining them together.
It's a presentation thing, but it's handled in the same fashion. You need to change the data type of the result to a string based one:
CAST((qty(S) + qty(B))/qty(id)*100 AS CHAR(2))+'%'