如何将数字格式化为 xxx-xx-xxxx?
我正在从存储过程查询社会安全号码数据,我想在存储过程中将其格式化为社会安全号码。
如何在 Oracle 中将 xxxxxxxxx 格式化为 xxx-xx-xxxx?
I am querying social security number data from a stored procedure and I would like to format it as a social security number in my stored procedure.
How can I format xxxxxxxxx like xxx-xx-xxxx in Oracle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 TO_CHAR 进行 SSN 格式化
更新:感谢 Gary 指出应使用“0”格式字符而不是“9”来保留前导零。
SSN formatting with TO_CHAR
update: thanks to Gary for pointing out that the '0' format character should be used rather than the '9' to preserve leading zeroes.
您还可以使用 concat 运算符
||
,这可能更具可读性。you could also use the concat operator
||
, which might be more readable.如果您想在应用格式之前检查该数字是否由 9 位数字组成,那么正则表达式可能会有所帮助:
问候,
抢。
And if you'd like to check if the number consists of 9 digits before applying the format, then regular expressions can be of help:
Regards,
Rob.
我只是晚了 11 年,但几个月前我就提出了这个问题,并且刚刚想出了我的新首选方法:
FM
修剪前导空格,replace()< /code> 将这些逗号变成破折号。 需要注意的是:这仅适用于数字(这在我们的用例中很好)。
它比
substr()
更优雅,比nls_numeric_characters
方法更紧凑(在我看来更容易记住)。也就是说,我认为此方法的效率低于 nls_numeric_characters 方法。
I'm only 11 years late, but I came to this question a few months ago and just figured out my new preferred method:
The
FM
trims the leading space, and thereplace()
turns those commas into dashes. One caveat: This only works for numbers (which is fine in our use-cases).It's more elegant than
substr()
and more compact (and easier to remember, in my opinion) thannls_numeric_characters
method.That said, I assume this method is less efficient than the
nls_numeric_characters
method.