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;
Related
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6
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
I have a zipcode stored in a text field (string) and would like to select only the last 3 digits of the value in my select statement. is this possible? Is there a standard way of doing this so that the SQL is interchangeable accross databases? I will be using it in production on Oracle, but i test on Interbase (yes, yes, i know, two totally diff DBs, but thats what i am doing)
thanks for any help you can offer
Assuming the zipcodes all have the same length, you can use substr.
If they don't have the same length, you have to do similar things with the strlen function.
Interbase does not have a built-in substring function, but it does have a UDF (user defined function) called SUBSTR in lib_udf.dll that works like this:
select substr(clients.lastname, 1, 10)
from clients
You declare the UDF like this:
DECLARE EXTERNAL FUNCTION SUBSTR
CSTRING(80),
SMALLINT,
SMALLINT
RETURNS CSTRING(80) FREE_IT
ENTRY_POINT 'IB_UDF_substr' MODULE_NAME 'ib_udf';
Oracle does have a built-in substr function that you use like this:
select substr(clients.lastname, 1, 10)
from clients
--jeroen
This depends on how your storing the zip code. If you are using 5 digits only
then this should work for Oracle and may work for Interbase.
select * from table where substr(zip,3,3) = '014'
IF you store Zip + 4 and you want the last 3 digits and some are 5 digits and some are 9 digits you would have to do the following.
select * from table where substr(zip,length(zip) -2,3) = '014'
and one option that may work better in both databases is
select * from table where zip like '%014'
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)
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6