how to round of decimal digit to nearest 50 - sql-server-2008

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.

Related

Mysql: if condition not gives unexpected value for float value

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

Remove trailing zeros only from int numbers

I have decimal column in db and trying to remove trailing zeros only from int numbers (100, 200, 300) with query in php. I tried with trim/cast functions but 10.50 turned 10.5.
SELECT TRIM(TRAILING '0' FROM `my_column`) FROM `mytable`
200.00
10.50
247.09
would display as
200
10.50
247.09
Use a case expression to determine if a number is an integer using ceil() and if it is format with zero decimals, if not with 2 decimals.
SELECT
CASE WHEN ceil(n) = n THEN format(n, 0) ELSE format(n, 2) END
FROM (
SELECT
1234.5 AS n
UNION ALL
SELECT
1234
) d

Format number to 2 decimal places

I would like to know how can I output a number with 2 decimal places, without rounding the original number.
For example:
2229,999 -> 2229,99
I already tried:
FORMAT(2229.999, 2)
CONVERT(2229.999, DECIMAL(4,2))
When formatting number to 2 decimal places you have two options TRUNCATE and ROUND. You are looking for TRUNCATE function.
Examples:
Without rounding:
TRUNCATE(0.166, 2)
-- will be evaluated to 0.16
TRUNCATE(0.164, 2)
-- will be evaluated to 0.16
docs: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate
With rounding:
ROUND(0.166, 2)
-- will be evaluated to 0.17
ROUND(0.164, 2)
-- will be evaluated to 0.16
docs: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round
You want to use the TRUNCATE command.
https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate
How about
CAST(2229.999 AS DECIMAL(6,2))
to get a decimal with 2 decimal places
Just use
format(number, qtyDecimals)
sample: format(1000, 2)
result 1000.00
This is how I used this is as an example:
CAST(vAvgMaterialUnitCost.`avgUnitCost` AS DECIMAL(11,2)) * woMaterials.`qtyUsed` AS materialCost
Show as decimal
Select ifnull(format(100.00, 1, 'en_US'), 0)
100.0
Show as Percentage
Select concat(ifnull(format(100.00, 0, 'en_US'), 0), '%')
100%

MySQL GROUP BY with GROUP_CONCAT return all row merged?

I ran into a problem today. I got a table with coins values :
0.01
0.01
0.01
0.05
0.10
0.25
1.00
1.00
2.00
and so on...
So the main idea is to get all the value one time so 0.01, 0.05, etc..
So I do :
SELECT Valeur FROM Mint_Coins GROUP BY Valeur
Now it will give me one row for each value... I want all values in one row so I use this :
SELECT GROUP_CONCAT(',', Valeur) AS Values FROM Mint_Coins GROUP BY Valeur
It give me again 7 rows in blob... to have blob it mean that the row are over 512 bytes... Okay, let's see when I convert that they contain... results is now :
0.01, 0.01, 0.01, 0.01
0.05, 0.05
etc..
So what I am doing wrong ? I want the result to hold in one colomn and one row like this 0.01,0.05,0.10,0.25,1.00,2.00.
Thanks
You can pass distinct to group_concat to ignore duplicated values:
SELECT GROUP_CONCAT(DISTINCT Valeur) AS Values FROM Mint_Coins
You also don't need to group by in this case.
If you wanted the results in multiple rows, you could also have done:
SELECT DISTINCT Valeur FROM Mint_Coins
SELECT GROUP_CONCAT(A.Valuer SEPARATOR ',') AS Valuer
FROM (SELECT DISTINCT Valuer FROM Mint_Coins)A ORDER BY Valuer;
Check this valuable link.This will help you.
http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/
Can you use a nested subquery?
SELECT GROUP_CONCAT(',', (SELECT Valeur FROM Mint_Coins GROUP BY Valeur)) AS Values FROM Mint_Coins

Break Numbers List Into Min and Max Ranges

Brain is not working today and my google skills are failing me.
I have a column of numbers ranging from 1 - 1000. I want to dump the min and max values for 100 (or whatever I chose) record ranges into a temp table. The plan is to use this temp table to process ranges of records (in this example 100 at a time) in a larger table.
Swear I have done this before with a CTE but then I had something to group on. Here I just want to break up a single list of numbers into ranges of X.
The output from the temp table should look like:
Min Max
0 99
100 199
200 299
300 399
etc.
Thanks!
You can use this trick from Stuart Ainsworth:
http://codegumbo.com/index.php/2009/01/25/building-ranges-using-a-dynamically-generated-numbers-table/
Numbers tables are awesome, but he uses a dynamically generated numbers table, which is even awesome...r.
If you know all numbers are present in the source table, you can use a recursive CTE to generate the number ranges:
; with numbers as
(
select 0 as a
, 99 as b
union all
select a+100
, b+100
from numbers
where a < 900
)
select *
from numbers
If the source table is sparsely populated, you can limit it to numbers that are actually present like:
... insert CTE from above here ...
select min(ot.NumberColumn)
, max(ot.NumberColumn)
from numbers
left join
OtherTable ot
on ot.NumberColumn between numbers.a and numbers.b
group by
numbers.a
enter code hereI have been having a play with a CTE after you posted this and came up with the following, I would be interested to hear if it works for you at all.
DECLARE #segment int = 100
;
WITH _CTE
(rowNum, value)
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY col01) -1, col01
FROM dbo.testTable
)
SELECT rowNum/#segment AS Bucket, MIN(Value) AS MinVal, MAX(Value) AS MaxVal
FROM _CTE
group by rowNum/#segment
ORDER BY Bucket
;
col01 in this case is the column that you want the min/max range values from, as is TestTable.