Setting decimal scale based on a column value - sql

I query two values from a table. I want to use the second value to set the decimal scale of the first value. Would anyone know how to set this using to_char? I was looking at the formats and I'm not sure how to make the int I'm receiving into the format I need. Here is my code:
with tmp as (select num, scale
from tl)
select fun, num
from fun_table f, tmp
where f.fun = tmp.scale;
EDIT:
if I receive num as 5.87 and scale as 3 I want the result to be 5.870
num: 7.00 scale: 1 expected output: 7.0
num: 7 scale: 4 expected output: 7.0000

Something like this?
SELECT TO_CHAR(num, CONCAT('99D',RPAD('9',scale,'9'))) FROM tl
or, if scale can be 0 and you don't want to show the decimal separator in such cases,
SELECT TO_CHAR(num, CONCAT('99',RPAD('D',scale+1,'9'))) FROM tl
Tested on Oracle Database 11g.

select round(fun,scale) from fun_table f, tmp where f.fun = tmp.scale;
Maybe this is what you're looking for? Depending on the logic you want you may want to replace round function with floor or ceil.

Related

selecting rows depending on the first digit of an integer in a column

Using SQL in PostgreSQL I need to select all the rows from my table called "crop" when the first digit of the integer numbers in column "field_id" is 7.
select *
from crop
where (left (field_id,1) = 7)
First, you know that the column is a number, so I would be inclined to explicitly convert it, no matter what you do:
where left(crop::text, 1) = '7'
where crop::text like '7%'
The conversion to text is simply to be explicit about what is happening and it makes it easier for Postgres to parse the query.
More importantly, if the value has a fixed number of digits, then I would suggest using a numeric range; something like this:
where crop >= 700000 and crop < 800000
This makes it easier for Postgres to use an index on the column.
Try with cast, like this:
select *
from crop
where cast(substring(cast(field_id as varchar(5)),1,1) as int) = 7
where 5 in varchar(5) you should put number how long is your integer.

Oracle round varchar2 column to 2 decimal places

I have a varchar2 column that i need to get an average number from.
So take the following table where val is a column varchar2(20):
val
5
4.654645
3.4534353
I have tried the following:
select ROUND(AVG(TO_NUMBER(val)),2) as n from table
The result i am getting is 1.9110E+13
The result i want is a number rounded to 2 decimal places
Thanks in advance for any help
Perhaps . . .
select CAST(AVG(TO_NUMBER(val)) as decimal(20, 2)) as n
from table;
Why are you storing numbers as strings? That seems like abad design decision.
So the following worked for me:
select ROUND(AVG(CAST((val) as decimal(20, 2))),2) as n from table
Thanks for suggestions

Convert scientific notation to float in BQ

Is there an easy way to convert a number in scientific notation to float in BigQuery?
For example:
8.32E-4 to 0,08
I know this is an older question, but for anyone else looking for a solution, this query (although a little clunky) should work for all scientific notation fields.
The difficulty is that BigQuery will convert any numbers greater than E±3 to scientific notation. The workaround is to cast the number as a string. If you're using this output for reporting purposes, it shouldn't be a problem. If you need to work with the numbers, make sure you've completed all calculations before converting:
SELECT
number
,base_num
,exponent
,CASE WHEN REGEXP_EXTRACT(number, r'E([+-])') = '+'
THEN REGEXP_EXTRACT(STRING((base_num * (POW(10, exponent)))), r'(\d+\.0)')
ELSE STRING(base_num * (POW(10, (exponent * -1))))
END AS converted_number
FROM
(SELECT
number
,FLOAT(REGEXP_EXTRACT(number, r'(.*)E')) base_num
,INTEGER(REGEXP_EXTRACT(number, r'E[+-](\d+)')) exponent
FROM
(SELECT "8E+1" AS number)
,(SELECT "1.6E-3" AS number)
,(SELECT "8.32E-4" AS number)
,(SELECT "2.92E+9" AS number)
)
Explanation:
FLOAT(REGEXP_EXTRACT(number, r'(.*)E')) returns the number before the E (as a string) and then converts it to a float.
INTEGER(REGEXP_EXTRACT(number, r'E[+-](\d+)')) returns the number after the E (as a string) and then converts it to an integer.
Then, raise 10 to the exponent (or its inverse, if it's negative) and multiply this by the base number.
Finally, cast as a string to maintain non-scientific notation. For numbers greater than 1, "round" to a single 0 after the decimal using regex for easier reading.
Output:
Row number base_num exponent converted_number
1 8E+1 8.0 1 80.0
2 1.6E-3 1.6 3 0.001600
3 8.32E-4 8.32 4 0.000832
4 2.92E+9 2.92 9 2920000000.0
I supposed this is for reporting purposes maybe? Does this solve for you?
SELECT FORMAT("%.10f", 8.32E-8) number UNION ALL
SELECT FORMAT("%.6f", 8.32E-4) UNION ALL
SELECT FORMAT("%4.f", 8.32E+4) UNION ALL
SELECT FORMAT("%10.f", 8.32E+8)

Oracle double conversion

I have this example:
CREATE TABLE test
(
VALUE NUMBER (5, 2)
);
INSERT INTO test
VALUES (6.3);
SELECT * FROM test;
In table test, I have a value of 6.3.
I have an application in .NET who queries this table and returns a single value of 6.3.
Assuming the value is stored in the s variable like this:
Dim s As Single = 6.3
Dim d As Double = CDbl(s)
.NET converts the single into a double, and the variable d has this value: 6.3000001907348633. I understand that is a different representation of the 6.3 value, but when I'm using this d value in a operation like this:
(795407.2 * d) / 100 = 50110,6551171188
in PL\SQL when I'm querying the table, I want to get the 6.3000001907348633 value instead of 6.3.
How can I convert this, or there is a datatype that does that? I've tried the BINARY_FLOAT and DOUBLE PRECISION types, but they don't convert this.
EDIT: Using 6.3 value I get: (795407.2 * d) / 100 = 50110,6536. if I round the value to get two decimal places (and assuming that this is a monetary value, I get 50110,66€ and 50110,65€ respectively).
Try using the BINARY_FLOAT Data Type in your SQL.
It represents a 32-bit floating point number in Oracle:
SQL> select cast(6.3 as BINARY_FLOAT) num,
2 to_char(cast(6.3 as BINARY_FLOAT),'0.00000000') num_to_char
3 from dual;
NUM NUM_TO_CHAR
---------- -----------
6,3E+000 6.30000019
Edit:
Sorry, I was too fast and didn't read all your trials
For compatibility purposes, try convert your number / literal to BINARY_FLOAT and then to BINARY_DOUBLE:
SQL> select TO_CHAR(cast(cast(6.3 as BINARY_FLOAT) as BINARY_DOUBLE),'0.000000000000000000') MyNum from dual;
MYNUM
---------------------
6.300000190734863300

Error to use 1.3 in sql query

I am using the following query.
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal).
but it returns the field number 2 result instead of 1.6
Please let me know how can I do it?
Here:
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal(2, 1))
You must specify number of digits in your decimal when casting, an number of digits after coma (I set them to 2 digits decimal with 1 after coma). You can easily test such casting by simply writing query:
SELECT Cast( 1.6 as decimal(2,1))
this will produce you effect of your casting. If you don't include (2,1) part, it will be automatically rounded to 2.