Find non-integer values in a float field - sql

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)

Related

SQL/MariaDB SELECT * FROM ... WHERE decimal number

I am having an issue with the SELECT statement. I want to do this:
SELECT * FROM `table1` WHERE x=0.0509
but it returns no rows. I know for sure that there is a row with an x value of 0.0509. If I do:
SELECT * FROM `table1` WHERE x=0
It does in fact return all rows where x=0, this means that my sql statement overall is correct. How do I SELECT where x is a number with decimals? I assume that the decimals is causing the issue?
maybe try balancing the search term between 2 values like this:
SELECT * FROM table1 WHERE x BETWEEN 0 AND 0.1;
This worked for me :)
Photo here
Edit: I can't find any way to get the precise decimal but you can probably just do this but make the values:
SELECT * FROM table1 WHERE x BETWEEN 0.05089999 AND 0.05090001;
This is probably your best way to do it, I'm pretty sure
The FLOAT is an approximate value. So, the stored value is not exactly 0.0509. You should use DECIMAL instead.
You can always convert the value to DECIMAL.
SELECT *
FROM table1
WHERE cast(x as decimal(10,4))=0.0509

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

SQL Server 2008 Calculation Bug

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)

PostgreSQL: IN A SINGLE SQL SYNTAX order by numeric value computed from a text column

A column has a string values like "1/200", "3.5" or "6". How can I convert this String to numeric value in single SQL query?
My actual SQL is more complicated, here is a simple example:
SELECT number_value_in_string FROM table
number_value_in_string's format will be one of:
##
#.##
#/###
I need to sort by the numeric value of this column. But of course postgres doesn't agree with me that 1/200 is a proper number.
Seeing your name I cannot but post a simplification of your answer:
SELECT id, number_value_in_string FROM table
ORDER BY CASE WHEN substr(number_value_in_string,1,2) = '1/'
THEN 1/substr(number_value_in_string,3)::numeric
ELSE number_value_in_string::numeric END, id;
Ignoring possible divide by zero.
I would define a stored function to convert the string to a numeric value, more or less like this:
CREATE OR REPLACE FUNCTION fraction_to_number(s CHARACTER VARYING)
RETURN DOUBLE PRECISION AS
BEGIN
RETURN
CASE WHEN s LIKE '%/%' THEN
CAST(split_part(s, '/', 1) AS double_precision)
/ CAST(split_part(s, '/', 2) AS double_precision)
ELSE
CAST(s AS DOUBLE PRECISION)
END CASE
END
Then you can ORDER BY fraction_to_number(weird_column)
If possible, I would revisit the data design. Is it all this complexity really necessary?
This postgres SQL does the trick:
select (parts[1] :: decimal) / (parts[2] :: decimal) as quotient
FROM (select regexp_split_to_array(number_value_in_string, '/') as parts from table) x
Here's a test of this code:
select (parts[1] :: decimal) / (parts[2] :: decimal) as quotient
FROM (select regexp_split_to_array('1/200', '/') as parts) x
Output:
0.005
Note that you would need to wrap this in a case statement to protect against divide-by-zero errors and/or array out of bounds issues etc if the column did not contain a forward slash
Note also that you could do it without the inner select, but you would have to use regexp_split_to_array twice (once for each part) and you would probably incur a performance hit. Nevertheless, it may be easier to code in-line and just accept the small performance loss.
I managed to solve my problem. Thanks all.
It goes something like this, in a single SQL. (I'm using POSTGRESQL)
It will sort a string coming in as either "#", "#.#" or "1/#"
SELECT id, number_value_in_string FROM table ORDER BY CASE WHEN position('1/' in number_value_in_string) = 1
THEN 1/substring(number_value_in_string from (position('1/' in number_value_in_string) + 2) )::numeric
ELSE number_value_in_string::numeric
END ASC, id
Hope this will help someone outhere in the future.