如何在数值

时间:2016-07-28 10:42:33

标签: sql oracle

不幸的是我在文档中找不到解决方案。

我希望获得特定格式的数字,例如:

234652.24  --> 234 652.24
42145124  --> 42 145 124

select employee_id, to_char(salary, '??????') as "Salary
from employees;

4 个答案:

答案 0 :(得分:3)

您可以指定the NLS_NUMERIC_CHARTACTERS setting作为the TO_CHAR() call的一部分,并使用GD format model占位符:

with employees (employee_id, salary) as (
  select 1, 234652.24 from dual --> 234 652.24
  union all select 2, 42145124 from dual --> 42 145 124
)
select employee_id,
  to_char(salary, '999G999G999D99', 'NLS_NUMERIC_CHARACTERS=''. ''') as "Salary"
from employees;

EMPLOYEE_ID Salary        
----------- ---------------
          1      234 652.24
          2   42 145 124.00

如果您不想在第二个值中使用尾随零,则可以添加FM格式修饰符,这也会删除前导空格(如果存在任何负值,则允许使用减号);但这仍然是落后期;您可以使用RTRIM()来摆脱它:

with employees (employee_id, salary) as (
  select 1, 234652.24 from dual --> 234 652.24
  union all select 2, 42145124 from dual --> 42 145 124
)
select employee_id,
  rtrim(to_char(salary, 'FM999G999G999D99', 'NLS_NUMERIC_CHARACTERS=''. '''), '.') as "Salary"
from employees;

EMPLOYEE_ID Salary        
----------- ---------------
          1 234 652.24     
          2 42 145 124     

答案 1 :(得分:0)

您绝对应该在https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_options072.htm

上阅读NLS_NUMERIC_CHARACTERS

答案 2 :(得分:0)

例如,

to_char( 1485, '9g999') ' 1 485' 
to_char( 148.5, '999.999')  ' 148.500' 
to_char( 148.5, '999d999')  ' 148,500' 
to_char( 3148.5,'9g999d999')    ' 3 148,500' 

答案 3 :(得分:-2)

我尝试了以下查询,输出以指定格式显示

mysql> select format(id, '## ###') from test;
+----------------------+
| format(id, '## ###') |
+----------------------+
| 123,456              |
| 123,456,789          |
| 1                    |
| 10                   |
| 10,000               |
+----------------------+
5 rows in set, 5 warnings (0.00 sec)