TSQL and Modulo - sql-server

How can I do this in T-Sql:
SQRT(id) % 1 = 0
I can't cast the result of the Sqrt() function because this renders the logic above useless.
Any Ideas on how I can achieve this?
Thanks

You could do something like this:
floor(sqrt(id)) = sqrt(id)

To see whether the sqrt is whole, try this: SQRT(id) * SQRT(id) = id. This might run into floating point precision issues though. I think this fixes them reliably for all numbers up to a certain threshold:
CONVERT(INT, SQRT(id)) * CONVERT(INT, SQRT(id)) = id
After the threshold, the precision will not be enough. You'll see false negatives but never false positives.

You could cast it to a varchar and then take the substring after the decimal point. More info here: http://dbaspot.com/sqlserver-programming/411976-how-get-numbers-after-decimal-tsql.html

Related

Formula returning zero instead of correct amount

SELECT (16/100)*(12200)
This query is returning 0 although it should return 1952. Why ?
Because 16/100 is integer, and it rounded off to zero.
Try
select CAST(16 AS DECIMAL(20,4))/100*(12200)
or better this way, to reduce rounding errors.
select CAST(16*12200 AS DECIMAL(20,4))/100

T-SQL Rounding, First Truncates to LENGTH + 2

In using the T-SQL ROUND function I noticed what seems like weird behavior. It looks like the ROUND function only looks at the first digit to the right of the digit to be rounded. If I round -6.146 to one decimal I get -6.1. I would have thought it would start at the right and round each digit as it works its way to the left, like this: -6.146 -> -6.15 -> -6.2
I've observed the same behavior with Excel’s round function too.
The query below illustrates what I am describing. I may simply use the nested ROUND functions as shown below but I'm curious if there’s a better way and which approach is considered mathematically correct.
DECLARE #Num AS FLOAT
SET #Num = -6.1463
SELECT #Num [OriginalVal], ROUND(#Num, 1, 0) [SingleRound]
, ROUND(ROUND(ROUND(#Num, 3, 0), 2, 0), 1, 0) [NestedRound]
Results
OriginalVal | SingleRound | NestedRound
-6.1463 | -6.1 | -6.2
I think the basic rule of thumb is, in rounding, you look at the 1 digit immediately to the right of the place you are rounding to. You do not extend it all the way to the very end of the right of the decimal.
http://math.about.com/od/arithmetic/a/Rounding.htm

How to get a float result by dividing two integer values using T-SQL?

Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like:
select 1/3
That currently returns 0. I would like it to return 0,33.
Something like:
select round(1/3, -2)
But that doesn't work. How can I achieve the desired result?
The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats first:
SELECT CAST(1 AS float) / CAST(3 AS float)
or
SELECT CAST(MyIntField1 AS float) / CAST(MyIntField2 AS float)
Because SQL Server performs integer division. Try this:
select 1 * 1.0 / 3
This is helpful when you pass integers as params.
select x * 1.0 / y
It's not necessary to cast both of them. Result datatype for a division is always the one with the higher data type precedence. Thus the solution must be:
SELECT CAST(1 AS float) / 3
or
SELECT 1 / CAST(3 AS float)
use
select 1/3.0
This will do the job.
I understand that CASTing to FLOAT is not allowed in MySQL and will raise an error when you attempt to CAST(1 AS float) as stated at MySQL dev.
The workaround to this is a simple one. Just do
(1 + 0.0)
Then use ROUND to achieve a specific number of decimal places like
ROUND((1+0.0)/(2+0.0), 3)
The above SQL divides 1 by 2 and returns a float to 3 decimal places, as in it would be 0.500.
One can CAST to the following types: binary, char, date, datetime, decimal, json, nchar, signed, time, and unsigned.
Looks like this trick works in SQL Server and is shorter (based in previous answers)
SELECT 1.0*MyInt1/MyInt2
Or:
SELECT (1.0*MyInt1)/MyInt2
Use this
select cast((1*1.00)/3 AS DECIMAL(16,2)) as Result
Here in this sql first convert to float or multiply by 1.00 .Which output will be a float number.Here i consider 2 decimal places. You can choose what you need.
If you came here (just like me) to find the solution for integer value, here is the answer:
CAST(9/2 AS UNSIGNED)
returns 5
I was surprised to see select 0.7/0.9 returning 0.8 in Teradata given they're already as floats/decimal numbers! I had to do cast(0.7 as float) to get the output that I was after.
When using literals, the best way is to "tell" SQL
which type you mean.
if you want a decimal result, add decimal point ".0" to your numbers:
SELECT 1.0 / 3.0
Result
0.333333
if you want a float (real) result, add "e0" to your numbers:
SELECT 1e0 / 3e0
Result
0.333333333333333

Wrong calculation in SQL-Server

As an example I found a simple calculation like:
select cast(200.00 as float) + 1908.30 + 170.00 + (-1150.00) + (-1128.30)
As a normal addition this results in 0.00 but SQL Server shows the result as 2.27373675443232E-13.
Why is this and how can I avoid this?
This error is inherent in the float datatype, and is the reason for the existence of decimal type. Never do money calculations as float values!
You can take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic, or any Google result for "float rounding error".
select cast(200.00 as decimal(10,2)) + 1908.30 + 170.00 + (-1150.00) + (-1128.30)
This is not unique to sql.
For example, in python:
'%.50f' % (200.+1908.3+170.-1150.-1128.30)
Will result in
'0.00000000000022737367544323205947875976562500000000'
Which is the same you got.
This is inherent to the inability to represent every number that can be expressed as a finite sum of powers of 10 (such as 1908.3) as a finite sum of positive and negative powers of 2, which is what representing it as a float does. An inevitable approximation occurs, and as you perform operations on said approximations, the final result may have a small error. The link provided by Amadan explains it.

Only display the values to the right of a Decimal Number

I use SQL Server 2005 and need to test whether values in a column that's metadata has been specified as DECIMAL(18.3) actually contains data that has values to the right of the Decimal point, and if so, what these values are.
I've read a few articles that only discuss how to drop off the decimal places or how to round the values, but not how to ONLY display what is stored to the right of the decimal point.
Your help would be greatly appreciated.
Kind Regards,
Ignacio.
Try:
SELECT a - FLOOR(a)
FROM ...
SELECT decimalnumber - FLOOR(decimalnumber) AS decimalpart
FROM mytable
WHERE decimalnumber - FLOOR(decimalnumber) > 0
This may not always work the way you expect it to. The problem occurs when you have negative numbers. You can think of FLOOR as a type of rounding, where it always rounds down to the next whole number. Floor(3.14) = 3, and Floor(-3.14) = -4.
To get the value of a number after the decimal point, you can use the ParseName function, which will work for positive and negative numbers.
Select ParseName(-3.9876, 1)
Select ParseName(-3.1234, 1)
Select ParseName(3.9876, 1)
Select ParseName(3.1234, 1)

Resources