Replace Function is rounding Up value in SQL

huangapple go评论81阅读模式
英文:

Replace Function is rounding Up value in SQL

问题

I've got a query

select distinct
r.max_range,	
convert(float,isnull(replace(r.max_range,0.0,100000000.0),100000000.0)) as max_amt,
convert(float,r.max_range) as 'convert_float',
replace(r.max_range,0.0,100000000.0) as 'replace_question'
from	#temp t1
join	LTR_Amounts r on (isnull(t1.amt,0) >= r.min_range 
and		isnull(t1.amt,0) <= convert(float,isnull(replace(r.max_range,0.0,100000000.0),100000000.0)))
where	r.category_id = 3
and		r.inactive <> 'y'

that produces the following -
I've got an amount 100,000 and it should fall into the

max_range max_amt convert_float replace_question
24999.99 25000 24999.99 25000
49999.99 50000 49999.99 50000
99999.99 100000 99999.99 100000
199999.99 200000 199999.99 200000

This can be run as follows

declare @max_range float = 99999.99
select distinct
@max_range,									convert(money,isnull(replace(@max_range,0.0,100000000.0),100000000.0)) as max_amt,
convert(money,@max_range) as 'convert_float',
replace(@max_range,0.0,100000000.0) as 'replace_question'
max_range max_amt convert_float replace_question
99999.99 100000.00 99999.99 100000

The issue at hand is if you can see everywhere as soon as I use replace as part of my query it rounds up the value. If I've got max_range of 99999.99 when I use replace as part of the formula it is looking at it as 1000000 but I need it to keep looking at it as 99999.99

the business rules in place require me to use replace (or some version of that) because sometimes the value is 0.0 and then I need it replaced with some maximum value.

How can I keep my formula as part of my join (with replace)

英文:

I've got a query

select distinct
r.max_range,	
convert(float,isnull(replace(r.max_range,0.0,100000000.0),100000000.0)) as max_amt,
convert(float,r.max_range) as &#39;convert_float&#39;,
replace(r.max_range,0.0,100000000.0) as &#39;replace_question&#39;
from	#temp t1
join	LTR_Amounts r on (isnull(t1.amt,0) &gt;= r.min_range 
and		isnull(t1.amt,0) &lt;= convert(float,isnull(replace(r.max_range,0.0,100000000.0),100000000.0)))
where	r.category_id = 3
and		r.inactive &lt;&gt; &#39;y&#39;

that produces the following -
I've got an amount 100,000 and it should fall into the

max_range max_amt convert_float replace_question
24999.99 25000 24999.99 25000
49999.99 50000 49999.99 50000
99999.99 100000 99999.99 100000
199999.99 200000 199999.99 200000

This can be run as follows

declare @max_range float = 99999.99
select distinct
@max_range,									convert(money,isnull(replace(@max_range,0.0,100000000.0),100000000.0)) as max_amt,
convert(money,@max_range) as &#39;convert_float&#39;,
replace(@max_range,0.0,100000000.0) as &#39;replace_question&#39;
max_range max_amt convert_float replace_question
99999.99 100000.00 99999.99 100000

The issue at hand is if you can see everywhere as soon as I use replace as part of my query it rounds up the value. If i've got max_range of 99999.99 when I use replace as part of the formula it is looking at it as 1000000 but I need it to keep looking at it as 99999.99

the business rules in place require me to use replace (or some version of that) because sometimes the value is 0.0 and then I need it replaced with some maximum value.

How can I keep my formula as part of my join (with replace)

答案1

得分: 1

首先,浮点数是近似值,即数据类型范围中并非所有值都可以精确表示 - 更多信息请参见这里

其次,REPLACE 接受字符或二进制数据类型作为参数,因此您的查询会隐式地将近似值转换为字符串。

我建议使用适当的固定数值数据类型,如decimal,并将 REPLACE 更改为类似 CASE 语句的东西。

英文:

First off, floats are approximate, i.e. not all values in the data type range can be represented exactly - see here for more info.

Secondly, REPLACE takes character or binary data types as arguments so your query is implicitly converting from an approximate value to a string.

I suggest using an appropriate fixed numeric data type like a decimal and changing out REPLACE for something like a CASE statement.

huangapple
  • 本文由 发表于 2023年2月9日 01:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定