如何在 Oracle SQL 查询中的数字中添加前导零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31693521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add leading zero in a number in Oracle SQL query?
提问by Madhusudan
I am retrieving a column named removal_count in my query using COUNT() function. In result set the datatype of removal_count is BIGDECIMAL. I want to convert number into five digits. SO if value is less than five digits then it should be represented with leading zero's.
我正在使用 COUNT() 函数在我的查询中检索名为 remove_count 的列。在结果集中,removal_count 的数据类型是 BIGDECIMAL。我想将数字转换为五位数字。所以如果值小于五位数,那么它应该用前导零表示。
e.g 1) If removal count is 540 then display 00540
2) If removal count is 60 then display 00060
例如 1) 如果移除计数为 540,则显示 00540
2) 如果移除计数为 60,则显示 00060
If the removal count is integer/string value then I can add leading zero's using java expression :
如果删除计数是整数/字符串值,那么我可以使用 java 表达式添加前导零:
--if removal_count is integer--
String.format("%05d",removal_count)
--if removal_count is string--
("00000"+removal_count).subString(removal_count.length())
Can we convert removal_count into string or integer ( from big decimal) so that I can use given java expression? Or else is there any way to add leading zero's in query itself?
我们可以将removing_count 转换为字符串或整数(从大十进制),以便我可以使用给定的java 表达式?或者有什么方法可以在查询本身中添加前导零?
回答by Lalit Kumar B
You could do it in two ways.
你可以通过两种方式做到这一点。
Method 1
方法一
Using LPAD.
使用LPAD。
For example,
例如,
SQL> WITH DATA(num) AS(
2 SELECT 540 FROM dual UNION ALL
3 SELECT 60 FROM dual UNION ALL
4 SELECT 2 FROM dual
5 )
6 SELECT num, lpad(num, 5, '0') num_pad FROM DATA;
NUM NUM_P
---------- -----
540 00540
60 00060
2 00002
SQL>
The WITH clause is only to build sample data for demo, in your actual query just do:
WITH 子句只是为演示构建示例数据,在您的实际查询中只需执行以下操作:
lpad(removal_count, 5, '0')
Remember, a numbercannot have leading zeroes. The output of above query is a stringand not a number.
请记住,数字不能有前导零。上述查询的输出是字符串而不是数字。
Method 2
方法二
Using TO_CHARand format model:
使用TO_CHAR和格式模型:
SQL> WITH DATA(num) AS(
2 SELECT 540 FROM dual UNION ALL
3 SELECT 60 FROM dual UNION ALL
4 SELECT 2 FROM dual
5 )
6 SELECT num, to_char(num, '00000') num_pad FROM DATA;
NUM NUM_PA
---------- ------
540 00540
60 00060
2 00002
SQL>
Update: To avoid the extra leading space which is used for minus sign, use FMin the TO_CHAR
format:
更新:为避免用于减号的额外前导空格,请按以下格式使用FMTO_CHAR
:
Without FM:
没有调频:
SELECT TO_CHAR(1, '00000') num_pad,
LENGTH(TO_CHAR(1, '00000')) tot_len
FROM dual;
NUM_PAD TOT_LEN
------- ----------
00001 6
With FM:
使用调频:
SELECT TO_CHAR(1, 'FM00000') num_pad,
LENGTH(TO_CHAR(1, 'FM00000')) tot_len
FROM dual;
NUM_PAD TOT_LEN
------- ----------
00001 5
回答by adamliesko
Use the LPAD
pl/sql function http://www.techonthenet.com/oracle/functions/lpad.php.
使用LPAD
pl/sql 函数http://www.techonthenet.com/oracle/functions/lpad.php。
removal_count = 540
LPAD(TO_CHAR(removal_count), 5, '0');
Result: '00540'
回答by Merk
In sqlplus you can use col format:
在 sqlplus 中,您可以使用 col 格式:
SQL> select 540 aa, 540 bb from dual ;
SQL> select 540 aa, 540 bb from dual ;
AA BB
540 540
SQL> col bb format 00000
SQL> col bb 格式 00000
SQL> /
SQL> /
AA BB
540 00540
SQL>
查询>