Multiply time with a decimal value in mysql? - mysql

I am trying to multiply a time with decimal value but not to get it right. That is, when I multiply with any decimal value, it is rounding that value to nearest integer and then doing the multiplication.
For Example: All the below operations are resulting the same value
1. '00:04:18' * 1.7 = '00:08:36'
2. '00:04:18' * 1.8 = '00:08:36'
3. '00:04:18' * 1.9 = '00:08:36'
4. '00:04:18' * 2 = '00:08:36'
Please let me know what change I have to do to get the accurate value without rounding.
Thanks

SEC_TO_TIME( TIME_TO_SEC( '00:04:18' ) * 1.7 ) = '00:07:18.6'
SEC_TO_TIME( TIME_TO_SEC( '00:04:18' ) * 1.7 ) = '00:07:44.4'

Related

Is there a ROUNDDOWN() function in sql as there is in EXCEL

Say I have a table which has two columns i.e. Quantity and Percentages where my percentages are in decimals. Now I want to multiply these two columns and Round the value down to 2 decimals. Rounding down here means that all the numbers from 1-9 are rounded down. Is there an inbuilt function in SQL to do so as there is in Excel?
Examples:
13.567 should round to 13.56
136.7834 should round to 136.78
0.7699 should round to 0.76
I have tried searching for such a function online but couldn't come across an appropriate solution.
There's a FLOOR function, which can be adapted to your use case:
SELECT FLOOR(value * 100) / 100 AS RoundedValue
You can use TRUNCATE () for this rounddown
select TRUNCATE(2.847, 2) as rounddown
or
SELECT Floor(135.675); //for integer rounding, like 135
You can also use
select round(123.456, 2, 1) as rounddown
The 3rd parameter being non-zero will cause a truncation after the number of decimal points specified in the 2nd parameter.
DB Fiddle
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/sqlref/src/tpc/db2z_bif_truncate.html
https://www.w3schools.com/sql/func_sqlserver_floor.asp
The solution to the problem is to truncate the extra decimal which can be achieved by using the extra parameter of the ROUND function which is ROUND(number, decimal_places, 0/1). Here if the last parameter is anything other than 0, it will truncate the rather than rounding off which is equivalent to the ROUNDDOWN() function of excel that I was looking for.
Alternatively, you can use the TRUNCATE() function, passing the number of decimal places to keep as the second parameter, which will drop off any extra decimals, acting as a ROUNDDOWN() function.
I hope this rounding utility helps somebody:
CREATE FUNCTION `get_round`(val DOUBLE, nDigits INT, RoundStyle VARCHAR(255)) RETURNS double
NO SQL
BEGIN
DECLARE a DOUBLE DEFAULT 0;
SET nDigits = ifnull(nDigits, 0);
CASE
WHEN UCASE(RoundStyle) IN ('ROUND NEAREST','', 'NEAREST', '', 'RND','ROUND', 'DEFAULT','DFLT', null) THEN #normal rounding, but up from 10.50#
SET a = round(val, nDigits);
WHEN UCASE(RoundStyle) IN('ROUND UP', 'UP') THEN #ROUND 10.554 to 10.56
SET a = ceil(val * (power(10, nDigits) )) / (power(10, nDigits));
WHEN UCASE(RoundStyle) IN('ROUND DOWN', 'DOWN') THEN #ROUND 10.555 to 10.55
SET a = truncate(val, nDigits) ;
WHEN UCASE(RoundStyle) IN('ROUND BANKER', 'BANKER','BANKERS ROUNDING') THEN #ROUND TO THE NEAREST EVEN 10.555 is 10.56 and 10.565 is 10.56
SET a = IF(ABS(val - TRUNCATE(val, nDigits)) * POWER(10, nDigits + 1) = 5
AND NOT CONVERT(TRUNCATE(ABS(val) * POWER(10, nDigits), 0), UNSIGNED) % 2 = 1,
TRUNCATE(val, nDigits), ROUND(val, nDigits));
WHEN UCASE(RoundStyle) IN('ROUND UP INTEGER', 'INT UP','UP INT') THEN #10.4 rounds to 11.0
SET a = ceiling(val);
WHEN UCASE(RoundStyle) IN('ROUND DOWN INTEGER', 'INT DOWN','DOWN INT') THEN #10.6 rounds to 10.0
SET a = floor(val);
END CASE;
RETURN ifnull(a, 0);
END
yes there are some Function in sql for round
ex:
SELECT ProductName, Price, FlOOR(Price) AS RoundedPrice
FROM Products;

Mysql SUM() TIME wrong format

I'm calculating the sum of two columns from the same table with SUM() but the end result is an integer (286676). I'm guessing it's milliseconds? How can I convert to TIME(00:00:00)?
database
id|hours_worked | hours_worked_wk2 |
hours_worked = 14:33:38
hours_worked_wk2 = 14:33:38
Query
SELECT *,SEC_TO_TIME(SUM(TIME_TO_SEC(ep.hours_worked)))+SEC_TO_TIME(SUM(TIME_TO_SEC(ep.hours_worked_wk2)))
AS TotalHoursWorked
FROM employeepayroll ep
JOIN employees em ON ep.employee_id=em.employee_id
JOIN payroll p ON ep.payroll_id=p.payroll_id
JOIN payrolltaxes pt ON ep.payroll_id=pt.payroll_id
WHERE ep.timesheet_status='Approved' AND p.pay_group='26'
ORDER BY ep.payroll_id DESC
TotalHoursWorkd = 286676
Use the following formula.
hours = cast(duration_in_milliseconds \ (60 * 60 * 1000) as int)
mins = (duration_in_milliseconds \ (60 * 1000)) mod 60
secs = (duration_in_milliseconds \ 1000) mod 60
Your query will look something like this:
select cast(duration_in_milliseconds\(60*60*1000) as int)+':'((duration_in_milliseconds \ (60*1000)) mod 60;)+':'((duration_in_milliseconds \ 1000) mod 60) from something
When you see it...
14:33:38 >>> 143,338 × 2 = 286,676
Yikes. The numbers in the times are being implicitly cast to integers.
You are adding the two values of SEC_TO_TIME(SUM(...))+SEC_TO_TIME(SUM(...)).
Instead, use SEC_TO_TIME(SUM(...) + SUM(...)).

How To Generate A Random Decimal Value In Mysql

I am trying to generate a random value between 0.01 - 0.50 to enter into mysql. I have 2.7 million rows that I need to execute this on.
Here is my script:
UPDATE FBAInventory SET buyBox = ROUND( 0.01 + RAND( ) * 8,2 );
It is generating values such as 4.20, 3.89 etc. I only want it to span from 0.01 - 0.50 and not to exceed this.
Does anyone know how to do this?
Thanks!
How about...
round(rand() * 0.49 + 0.01, 2);
You can use the floor function to generate a range of random numbers.
FLOOR(RAND() * (<max> - <min> + 1)) + <min>
where the max and min are inclusive. So in your case you would want
FLOOR(RAND() * 1.49 ) + 0.01

The Decimal Data Type in SQL Server

Why does the following return .500000 rather than .500?
SELECT 50 / CAST(100 AS decimal(3))
Because you are casting the divisor not the result. And you have not specified precision.
See this for examples:
SELECT 50 / CAST(100 AS decimal(3))
,50 / CAST(100 AS decimal(6,3))
,CAST (50 / CAST(100 AS decimal(3)) AS decimal(3))
,CAST (50 / CAST(100 AS decimal(3)) AS decimal(6,3))
Precision, Scale, and Length (Transact-SQL)
Precision is the number of digits in a number. Scale is the number of
digits to the right of the decimal point in a number. For example, the
number 123.45 has a precision of 5 and a scale of 2.
DECIMAL uses a default precision of 18 and scale of 3.
Therefore when you case to DECIMAL(3) you get a DECIMAL(3,0). Then you use it as a divisor and SQL Server follows its rules for the resultant scale.
Operation Result precision Result scale
e1 / e2 p1 - s1 + s2 + max(6, s1 + p2 + 1) max(6, s1 + p2 + 1)

SQL Server - single Inline Query - (decimal remainder of x/y (rounded to 6 characters) ) / z

Can I ask for help on a SQL Statement please, I have to do the calculation inline and cannot declare variables for it
Calculation:
-91000000 / 2700000 = -33.7037037037
I need the remainder (7037037037 - but only up to 6 characters ) to be multiplied by 15000
703703 / 15000 = Final Answer of 49.913533
I thought I could do this:
select cast(ParseName(abs(cast(-91000000 as decimal)/ 2700000 ) %1,1) as numeric(8,8)) / 15000
WITH cte AS
(
SELECT -91000000 AS x, 2700000 AS y
)
SELECT ABS(ROUND((CAST(x AS decimal) / CAST(y AS decimal)) - (x/y), 6)) * 1000000 / 15000 FROM CTE