将逗号转换为数字并限制为小数点后 3 位
Transforming comma to number and limiting to 3 decimal places
我的值结构如下:
0,132
6,0999999999999999E-2
我希望它变成这样:
0.132
0.060
在 Oracle 数据库中使用 sql 查询?
把逗号变成点,只保留 3 个小数点。尝试了 CAST 和 TO_NUMBER 格式化,但没有用。
您可以像这样使用 ORACLE 的 to_number、替换和舍入函数:
SELECT round(to_number(replace(string,',','.')),3) FROM dual
您还可以使用 the to_number()
function 的可选第三个参数来指定逗号作为小数点分隔符。使用 CTE 提供您的字符串值:
with t (str) as (
select '0,132' from dual
union all select '6,0999999999999999E-2' from dual
)
select to_number(str, '9D9999999999999999EEEE', 'NLS_NUMERIC_CHARACTERS='',.''')
as num
from t;
NUM
----------
.132
.061
格式模型需要在小数点后有足够的数字,以便在您的 table 中获得最精确的数字。 EEEE
format 处理科学记数法。
然后您可以使用round()
or trunc()
来限制小数点后三位;四舍五入对您的样本值没有影响,但截断显示第二个值从 0.061 变为 0.060:
with t (str) as (
select '0,1322' from dual
union all select '6,0999999999999999E-2' from dual
)
select trunc(to_number(str, '9D9999999999999999EEEE',
'NLS_NUMERIC_CHARACTERS='',.'''), 3) as num
from t;
NUM
----------
.132
.06
如果您愿意,可以使用 to_char()
(或您的 client/application)来显示前导和尾随零。
您还可以将会话 NLS_NUMERIC_CHARACTERS 设置为 ',.'
,但更安全的做法是不要假定任何人 运行 此查询将具有特定设置并将其作为查询的一部分。
我的值结构如下:
0,132
6,0999999999999999E-2
我希望它变成这样:
0.132
0.060
在 Oracle 数据库中使用 sql 查询?
把逗号变成点,只保留 3 个小数点。尝试了 CAST 和 TO_NUMBER 格式化,但没有用。
您可以像这样使用 ORACLE 的 to_number、替换和舍入函数:
SELECT round(to_number(replace(string,',','.')),3) FROM dual
您还可以使用 the to_number()
function 的可选第三个参数来指定逗号作为小数点分隔符。使用 CTE 提供您的字符串值:
with t (str) as (
select '0,132' from dual
union all select '6,0999999999999999E-2' from dual
)
select to_number(str, '9D9999999999999999EEEE', 'NLS_NUMERIC_CHARACTERS='',.''')
as num
from t;
NUM
----------
.132
.061
格式模型需要在小数点后有足够的数字,以便在您的 table 中获得最精确的数字。 EEEE
format 处理科学记数法。
然后您可以使用round()
or trunc()
来限制小数点后三位;四舍五入对您的样本值没有影响,但截断显示第二个值从 0.061 变为 0.060:
with t (str) as (
select '0,1322' from dual
union all select '6,0999999999999999E-2' from dual
)
select trunc(to_number(str, '9D9999999999999999EEEE',
'NLS_NUMERIC_CHARACTERS='',.'''), 3) as num
from t;
NUM
----------
.132
.06
如果您愿意,可以使用 to_char()
(或您的 client/application)来显示前导和尾随零。
您还可以将会话 NLS_NUMERIC_CHARACTERS 设置为 ',.'
,但更安全的做法是不要假定任何人 运行 此查询将具有特定设置并将其作为查询的一部分。