不幸的是,我無法在文檔中找到解決方案。如何在數值中添加分隔空白空間
我想在特定的格式得到的數字,像這樣:
234652.24 --> 234 652.24
42145124 --> 42 145 124
select employee_id, to_char(salary, '??????') as "Salary
from employees;
不幸的是,我無法在文檔中找到解決方案。如何在數值中添加分隔空白空間
我想在特定的格式得到的數字,像這樣:
234652.24 --> 234 652.24
42145124 --> 42 145 124
select employee_id, to_char(salary, '??????') as "Salary
from employees;
可以指定the NLS_NUMERIC_CHARTACTERS
setting爲部分the TO_CHAR()
call,並使用G
和D
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
你一定要了解NLS_NUMERIC_CHARACTERS
在https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_options072.htm
例如,
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'
我使用SQL Developer和當我使用你的答案我不沒有空白但是,。所以這不是我的問題的解決方案。 – hakobot
在運行之前,你已經錯過了你修改session set nls_numeric_characters =',''的位置。它也不會給OP的第二個樣本值提供期望的結果。 –
我嘗試下面的查詢和輸出顯示在指定的格式
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)
針對Oracle問題的MySQL解決方案?爲什麼? – mathguy
問題是關於Oracle,而不是MySQL。 –
似乎她是困惑..即使我相同:(:((總是更好地回答tsql問題,而不是Sql :) –
謝謝你的回答。我有我想要的甚至更多:) – hakobot