SQL Server 2008 Calculation Bug - sql

Here is the query i am running
Select (100/18)*18
The answer i get is 90. I have tried declaring each of the numbers as decimals also, and i get the value 100.0008, but when i calculate with variables as floats the answer is correct at '100' so does anyone have any idea why SQL calculates like this?

Because it will first evelute the paranthesis
So
Select (100/18)*18
will (100/18) = 5.555555 but it assume both number as int so it will cast the result as int so it will return 5
so now 5*18 = 90
If you want the correct result do this instead Select (100*18)/18

to get the desired result you should try something like this:
Select CEILING((100/18.0)*18)
when you do this,
Select (100/18)*18
sql server consider operation as integer division and take select 100/18 as 5 instaed of 5.55555....

Try to express the numders ac decimal numbers
SELECT (100.0/18)*18
and
SELECT ROUND((100.0/18)*18,2)
...

you can try this
select abs(18*100/18)

Related

SQL Decimal points to be remove extra values.?

I have some case about 8.89291 value in SQL database.
In this case if I'm running:
select cast(ROUND(8.89291,0) as float)
The output is : 9
What I'm actually looking for is
The output is : 8.9
How can I get this value?
Will you guys please help me?
Your precision is 0 so its giving the whole value.
select cast(round(8.89291, 1) as float)
If you are open to casting, then you don't also need to call ROUND. Consider casting to NUMERIC(10,1):
SELECT CAST(8.89291 AS NUMERIC(10,1));
Demo
(demo for SQL Server, but the above would run on many other databases as well)
You had selected 0 that mean float function will treat as Integer ,for the float you have to put 1 instead of 0 ,please check below one.
select cast(ROUND(8.89291,1) as float)
You can use
SELECT CAST(ROUND(8.89291,2) AS NUMERIC(12,1))
or
SELECT CAST(8.89291 AS NUMERIC(12,1))

Get value till 5 digits in SQL

I am trying to run a SQL query and its output of a column should contain value till 5 decimals but it's by default rounding till 2 digits. I am using the below given query. Can someone please help me with this.
declare #VITG decimal(18,5)
select Left(wount(#VITG,18,5)) as VITG
from DBFFile
select CAST(MyNymber AS DECIMAL(18,5))
This will work
select convert(decimal(18,5),ColumnName) as VITG
from DBFFile
As your not using T-sql Datasourse you should do this is code for ex in c# you have to do some thing like this.
var columnValue=From_Query_Result_As_String;
if(columnValue.Substring(columnValue.IndexOf(".")+1).Length>=5)
{
columnValue=Convert.ToDecimal(columnValue.Substring(0,columnValue.IndexOf(".")+6));
}

How to find values with certain number of decimal places using SQL?

I'm trying to figure out a way, using SQL, to query for values that go out to, say, 5 or more decimal places. In other words, I want to see only results that have 5+ decimal places (e.g. 45.324754) - the numbers before the decimal are irrelevant, however, I still need to see the full number. Is this possible? Any help if appreciated.
Assuming your DBMS supports FLOOR and your datatype conversion model supports this multiplication, you can do this:
SELECT *
FROM Table
WHERE FLOOR(Num*100000)!=Num*100000
This has the advantage of not requiring a conversion to a string datatype.
On SQL Server, you can specify:
SELECT *
FROM Table
WHERE Value <> ROUND(Value,4,1);
For an ANSI method, you can use:
SELECT *
FROM Table
WHERE Value <> CAST(Value*100000.0 AS INT) / 100000.0;
Although this method might cause an overflow if you're working with large numbers.
I imagine most DBMSs have a round function
SELECT *
FROM YourTable
WHERE YourCol <> ROUND(YourCol,4)
This worked for me in SQL Server:
SELECT *
FROM YourTable
WHERE YourValue LIKE '%._____%';
select val
from tablename
where length(substr(val,instr(val, '.')+1)) > 5
This is a way to do it in oracle using substr and instr
You can use below decode statement to identify maximum decimal present in database table
SELECT max(decode(INSTR(val,'.'), 0, 0, LENGTH(SUBSTR(val,INSTR(val,'.')+1)))) max_decimal
FROM tablename A;

how to retrieve values that are fractional from a column in SQL

I have a column named quantity with values 1.00,2.00,1.5,2.5,etc. If I want to retrieve the values 1.5,2.5 along with all other similar values how can I write the query?
in SQL in have tried:
SELECT quantity_billed FROM invoice_line_item WHERE quantity_billed != ROUND(quantity_billed)
and
with like operator it is giving the values like 1.00 also.
You can use a MODULO function, too:
SELECT quantity_billed
FROM invoice_line_item
WHERE quantity_billed MOD 1 <> 0
%LIKE% only works for Character string,so you should convert to varchar.if ur data in float or decimal.
SELECT quantity_billed
FROM invoice_line_item WHERE convert (varchar,quantity_billed )
like '%.5%'
How about using NOT LIKE('%.00') to get fractional amounts. Not sure which database you are using so you may need to modify slightly but this works in MySql and SQL Server

Find non-integer values in a float field

I'm having trouble finding a way to write a query that will return all non-integers in a float column in SQL Server 2005/8.
I have a float field where the majority of the data in it is actually integers, but I'd like to take a look at the rows where the values actually contain a decimal value. The first thing I tried was modulus 1, but the % operator doesn't work on float values.
Thanks for your help!
I don't know exact syntax of MSSQL, however you could try something like that (pseudo-code)
SELECT ... FROM tbl_name WHERE col_name != CAST(col_name AS INTEGER)
are you just wanting the rows with a decimal in it?
select field
from table
where field like '%.%'
Try something like:
SELECT *
FROM MyTable
WHERE (CONVERT(INT, floatField) - floatField) <> 0
You may also try this:
SELECT * FROM tbl WHERE col != ROUND(col)