Mysql: if condition not gives unexpected value for float value - mysql

My mysql query is like this
select if(brand_name="royal", b.multiplier, 1) as multiplier from brand;
my database have below record
brand_name = royal,
multiplier = 1.06 (type: float(5,2))
in result multiplier value should be 1.06 but it give value in result as : 1.0599999427795
Please can you tell me how I can resolve this issue ?

This is just the way floats behave:
The correct solution is to use DECIMAL(M, D) datatype.

if you want to fix the decimal point value, need to change type of multiplier from float -> decimal
Then use this code
SELECT IF( brand_name = "apple", multiplier, 1 ) AS multiplier
FROM stack
LIMIT 0 , 30

Related

How to bitshift negative numbers with mySQL to get results like in Java, Python etc?

When i want to bitshift -2 >> 4 it should give me -1. python and java do give me -1. But if i try it on my mySQL server i get 1152921504606846975. I tryed to inverse the bits to cast it etc But i am not able to get a -1. So does someone know how to bitshift it to get -1.
According to the documentation, MySQL's bit shift operators generate an unsigned 64-bit integer. Therefore, if you want to get the expected behavior with negative numbers, you may add your own logic:
WITH yourTable AS (
SELECT 4 AS val UNION ALL
SELECT -4
)
SELECT
IF(val > 0, val >> 2, -1.0*((-1.0*val) >> 2)) AS result
FROM yourTable;
This outputs:
1
-1.0
>>:
Shifts a longlong (BIGINT) number to the right.
The result is an unsigned 64-bit integer. The value is truncated to 64 bits. In particular, if the shift count is greater or equal to the width of an unsigned 64-bit number, the result is zero.
-1 does not belong to the range of unsigned integer.

how to round of decimal digit to nearest 50

How can it convert or round off the decimal digit to nearest 50. for e.g if i get 2.00 to 2.49 then it may change to 2.50 ,
2.50 to 2.99 then it may change to 3.00.
pls solve if anyone knows.
Something like this should produce your required rounding up:
SELECT CEILING(<input> * 2.0) / 2.0
Where <input> is the column or expression that's currently producing the values you want to round.
I don't know if this is an efficient way or not but I tried this :
IF OBJECT_ID('MyTable','U') IS NOT NULL
DROP TABLE MyTable
GO
CREATE TABLE MyTable (num DECIMAL(10,2))
GO
INSERT INTO MyTable
values (1.00),(1.01),(1.49),(1.50),(1.51),(1.99),(2.00),
(11.00),(11.01),(11.49),(11.50),(11.51),(11.99),(12.00)
SELECT [num],
CASE
WHEN ( [num] - [Nbr] ) BETWEEN 0.01 AND 0.49 THEN [Nbr] + 0.5
ELSE [Nbr]
END AS [Result]
FROM (SELECT [num],
ROUND(num, 0) AS [Nbr]
FROM MyTable) t
Note: Case when condition can be modified as per requirement.

Cast as decimal in mysql

I have below table structure and data :
create table sample
(
id INT(10)
);
INSERT INTO sample
values
(23398),
(98743),
(54734);
Now I want to understand CAST function in mysql. Consider following query :
select
cast((id/3) as decimal(2,2)) as cast1,
cast((id/3) as decimal(3,2)) as cast2,
cast((id/3) as decimal(4,2)) as cast3,
cast((id/3) as decimal(5,2)) as cast4,
cast((id/3) as decimal(6,2)) as cast5,
cast((id/3) as decimal(7,2)) as cast6,
id/3 as actualId
from sample;
Please see output of this query at SQL Fiddle.
I am wondering why this query gives 0.99, 9.99 and vice versa.
Can anyone explain it ?
Thanks in advance.
decimal is a type that takes 2 arguments
decimal(size, places) :
size determines how many digits are in the number.
places determines how many of those digits are to the right of the decimal.
decimal(2,2) - .00 - 2 digits both of which are to the right of the decimal
when casting (23398 / 3) = 7799.33333333 to declimal(2, 2) it yields a decimal in the specified amount of space closest to the desired number which is 0.99
decimal(3,2) - 0.00 - 3 digits 2 of which are to the right of the decimal
when casting (23398 / 3) = 7799.33333333 to declimal(3, 2) it yields a decimal in the specified amount of space closest to the desired number which is 9.99
if all of the original numbers were negative you would yield -0.99 and -9.99 because they are the closest numbers to the desired number within the allocated space
As a matter of fact java does something similar if you take the max double and try to convert it to an int you will give the max int which is no where near the max double

BETWEEN operator working wrong in MySQL

I have a query having BETWEEN operator but it showing wrong results
My query-
SELECT * FROM register WHERE height BETWEEN '1' AND '6'
It also shows the user with height 10, 12 and 16 which is wrong. What is the problem with this query?
I have another query which work fine but is not proper way of using as it make query lengthy
SELECT * FROM register WHERE height > 1 AND height < 12
Give me idea for right way of getting the query as if more condition is added it would be hard for the query to understand and code.
Assuming height is an integer should it not be
SELECT * FROM register WHERE height BETWEEN 1 AND 6
You don't need the single quotes
If:
1. You can't or don't want to change the column type
2. The charachters in the field are only numbers
You can change your query to:
SELECT *
FROM register
WHERE CONVERT(height, UNSIGNED INTEGER) BETWEEN 1 AND 6
See my example at this SQL fiddle.
Check the data type of your column 'Height' it should be a int or float or double and if it is amongs above run the following query
SELECT * FROM register WHERE height BETWEEN 1 AND 6
If the height field is not numeric, you could convert it before making you comparison.
SELECT * FROM register WHERE CONVERT(height, unsigned) BETWEEN 1 AND 6

Why doesn't this sql query return any results comparing floating point numbers?

I have this in a mysql table:
id and bolag_id are int. lat and lngitude are double.
If I use the the lngitude column, no results are returned:
lngitude Query: SELECT * FROM location_forslag WHERElngitude= 13.8461208
However, if I use the lat column, it does return results:
lat Query: SELECT * FROM location_forslag WHERElat= 58.3902782
What is the problem with the lngitude column?
It is not generally a good idea to compare floating point numbers with = equals operator.
Is it correct to compare two rounded floating point numbers using the == operator?
Dealing with accuracy problems in floating-point numbers
For your application, you need to consider how close you want the answer to be.
1 degree is about 112km, and 0.00001 degrees is about 1.1 metres (at the equator). Do you really want your application to say "not equal" if two points are different by 0.00000001 degrees = 1mm?
set #EPSLION = 0.00001 /* 1.1 metres at equator */
SELECT * FROM location_forslag
WHERE `lngitude` >= 13.8461208 -#EPSILON
AND `lngitude` <= 13.8461208 + #EPSILON
This will return points where lngitude is within #epsilon degrees of the desired value.
You should choose a value for epsilon which is appropriate to your application.
Floating points are irritating....
WHERE ABS(lngitude - 13.8461208) < 0.00000005
Convert float to decimal for compare. I had the same problem and solved like this:
SELECT
[dbo].[Story].[Longitude],
[dbo].[Story].[Latitude],
[dbo].[Story].[Location],
FROM
[dbo].[Story],
[dbo].[Places]
WHERE
convert(decimal, [dbo].[Story].[Latitude]) = convert(decimal, [dbo].[Places].[Latitude])
and
convert(decimal, [dbo].[Story].[Longitude]) = convert(decimal, [dbo].[Places].[Longitude])
and
[dbo].[Places].[Id] = #PlacesID
and
[dbo].[Story].IsDraft = 0
ORDER BY
[dbo].[Story].[Time] desc
Look at the first 3 rows after the WHERE clausule.
Hope it helps.