what is difference between ceiling and roundup server in sql server - sql

What is difference between the Ceiling and Round functions in in SQL Server?
I have some query and I get a totally different value in Round and Ceiling function.

The answer is here
Round does a standard rounding. If value is .5 or over then you get back 1. If it’s less than .5 you get back 0
Ceiling returns the integer equal to or higher than the value passed in.
SELECT ROUND(235.400,0);
Answer= 235.000
SELECT CAST(ROUND(235.400,0) as int)
Answer= 235

Round allows decimal values to round the value.
It will take next value if decimal value is only greater than or equal to 5.
Ceiling no need to decimal values.
It will take the next value of the given number. If decimal value is even less than 5.

ROUND let's you round values in a standard way (round up from values 5 or higher, round down otherwise). It also takes number of decimal places you want to round to, so if you want to get an integer, you just pass 0 as number of decimal places. See documentation.
CEILING is operation, which return the smallest integer greater than passed number, so it rounds up to next integer.
CONLUSION:
So basic difference: CEILING rounds up, while ROUND rounds number in standard way.
Another key difference is that ROUND let's you specify number of decimal places you want round to.

Related

How to calculate decimal(x, y) max value in SQL Server

How do I know the maximum value of decimal type?
For example:
decimal(5, 2)
Can you please explain the mechanism of decimal type?
The maximum possible value can be calculated using the following maths:
(10 ^ (x-y)) - (10 ^ -y)
So in your initial example
(10 ^ (5-2)) - (10 ^ -2) = 1000 - 0.01 = 999.99
I'm adding this answer as my google results took me here when trying to remind myself how to calculate the SQL friendly upper bounds of a C# decimal going into an SQL (28,10) decimal field.
999999999999999999.9999999999 by the way.
You can use an aggregate function in order to get the maximum value in a column
SELECT MAX(myColumn) AS MyColumnMax
FROM myTable
If you want to know the maximum value for each customer for instance, you can group by this customer
SELECT CustomerID, MAX(OrderAmount) AS MaxOrderAmount
FROM CustomerOrders
GROUP BY CustomerID
You can find other aggregate functions here.
If, by contrast, you are interested in range of a decimal type, then consider, that you are declaring the total number of digits and decimals. Therefore the maximum number is reached, when all these digits are 9.
So for decimal(5,2) it would be 999.99. 5 is the total number of decimals to the left and to the right of the decimal point. 2 is the number of decimals to the right of the decimal point.
The maximum possible range for decimals is -10^38 + 1 through 10^38 - 1.

SQL Doubles confused

What does double [3,2] mean? Is it formatting?
double [3,2] means than values can be stored with up to 3 digits in total, of which 2 digits may be after the decimal.
The maximum number of digits may be specified as the first parameter.
The maximum number of digits to the right of the decimal point is specified in the last parameter.

SQL Server ROUND based on decimal values

How to to round the decimal values in SQL Server 2008 R2?
I need to round the value based on the decimal points.
Logic
If the decimal value greater than or equal to 0.5 then it should be rounded to the nearest upper number.
If the value is lesser than 0.5 then the value should be rounded to nearest lower number.
I can't use neither upper alone nor lower alone. It should be based on the value.
For example:
if the data comes as 1.569 it should be rounded to 2 (i.e. 1.5 to 1.99)
if the data comes as 1.321 it should be rounded to 1 (i.e. 1.01 to 1.49)
Use round Function
Example select convert(int,(ROUND(1.5,0)))
o/p 2
For Info Please Refer This Link.......
http://technet.microsoft.com/en-us/library/ms175003%28v=sql.110%29.aspx
Use the ROUND function
SELECT ROUND(data) FROM table

How can I round up to the next multiple of 0.05 using SQL Server?

How can I round a floating-point number to the hundredth place, while also rounding up to the nearest multiple of 0.05 in SQL Server.
Example 4.93 would round to 4.95
Multiply it by 20, then round it to next integer, then divide by 20.
You can use this formula, where #round_to is the number to a multiple of which you'd like to round. #round_updown chooses up or down rounding: set it to 0 to round down, to #round_to - 0.000001 to round up, or to #round_to / 2 to use middle rounding.
select #round_to*cast((#value+#round_updown)/#round_to as int)
For example:
select 0.05*cast((4.93+0.025)/0.05 as int)

Database field definitions

I need to do a data migration from a data base and I'm not too familiar with databases so I would like some clarification. I have some documentation that could apply to either an Oracle or a SQL database and it has a column defined as NUMBER(10,5). I would like to know what this means. I think it means that the number has 10 digits with 5 after the decimal point, but I would like clarification. Also would this be different for either SQL or Oracle?
The first number is precision the second number is scale. The equivalent in SQL Server can be as Decimal / Numeric and you could define it like so:
DECLARE #MyDec decimal(18,2)
The 18 is the max total number of decimal digits that can be stored (that is the total number of digits, for instance 123.45 the precision here is 5, while the scale is 2). The 2 is the scale and it specifies the max number of digits stored to the right of the decimal point.
See this article
Just remember the more precision the more size in storage bytes. So keep it at a minimum if possible.
p (precision)
Specifies the maximum total number of
decimal digits that can be stored,
both to the left and to the right of
the decimal point. The precision must
be a value from 1 through the maximum
precision. The maximum precision is
38. The default precision is 18.
s (scale)
Specifies the maximum number of
decimal digits that can be stored to
the right of the decimal point. Scale
must be a value from 0 through p.
Scale can be specified only if
precision is specified. The default
scale is 0; therefore, 0 <= s <= p.
Maximum storage sizes vary, based on
the precision.
Finally, it is worth mentioning that in oracle you can define a scale greater then a precision, for instance Number(3, 10) is valid in oracle. SQL Server on the other hand requires that the precision >= scale. So if you defined Number(3,10) in oracle, it would map into sql as Number(10,10).
Defining a column in Oracle as NUMBER(10,5) means that the column value can have a decimal of up to five places of precision, and ten digits in overall length. If you insert a value into the column that does not have any decimal places defined, the maximum the column will support is 10 digits. For example, these values will be supported by the column defined as NUMBER(10,5):
1234567890
12345.67890
It made validation a pain.
MySQL and SQL Server don't support the NUMBER data type - to support decimals, you're looking at using DECIMAL (or FLOAT?). I haven't looked at PostgreSQL, but I would figure it to be similar to Oracle.
In Oracle, a column defined as NUMBER(4,5) requires a zero for the first digit after the decimal point and rounds all values past the fifth digit after the decimal point.
From the Oracle documentation
NUMBER(p,s)
where: p is the precision, or the
total number of digits. Oracle
guarantees the portability of numbers
with precision ranging from 1 to 38. s
is the scale, or the number of digits
to the right of the decimal point. The
scale can range from -84 to 127.
Here are some examples :
Actual data .000127 stored in NUMBER(4,5) becomes .00013
Actual data 7456123.89 stored in NUMBER(7,-2) becomes 7456100
Edited
JonH mentions something noteworthy:
Oracle allows the scale > precision,
so SQL will map that so that if s>p
then p becomes s. That is NUMBER(3, 4)
in oracle becomes NUMERIC(4,4) in SQL.