string Oracle,向字符串(不是数字)添加前导零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23814518/
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
Oracle, adding leading zeros to string (not number)
提问by Ovi
I am using Oracle (work space is TOAD) and I need to make my strings that if they are shorted then 10 characters then add leading zeros to make them all 10 digit strings.
我正在使用 Oracle(工作空间是 TOAD)并且我需要制作我的字符串,如果它们被缩短,则为 10 个字符,然后添加前导零以使它们全部为 10 位字符串。
For example if I have a string like this: '12H89' need to be '0000012H89' or '1234' to be '0000001234'
例如,如果我有一个这样的字符串:'12H89' 需要是 '0000012H89' 或 '1234' 是 '0000001234'
How can this be done? Whats the best way?
如何才能做到这一点?最好的方法是什么?
Thanks in advance .
提前致谢 。
回答by GolezTrol
You can use the LPAD function for that, passing in the string, the length you want it to be, and the character to pad it with. For 10 digits with leading zeroes this would be:
您可以为此使用 LPAD 函数,传入字符串、您想要的长度以及填充它的字符。对于带有前导零的 10 位数字,这将是:
LPAD('12H89', 10, '0')
The return value is the padded string.
返回值是填充后的字符串。