使用 to_char 格式化间隔
以下 SQL 命令
select TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))) from table1
生成以下格式的结果:+000000000 00:03:01.954000。
是否可以在 to_char 函数中输入特殊格式以获得 格式结果:+00 00:00:00.000?
Following SQL command
select TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))) from table1
produces a result of the format: +000000000 00:03:01.954000.
Is it possible to enter a special format in the to_char function in order to get
a result of format: +00 00:00:00.000?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想要降低精度,您可以转换结果:
编辑以下OP评论:
来自 Oracle 文档 (11gr1) :
看来您必须手动使用 EXTRACT 才能实现所需的输出:
这不是很优雅,但似乎这是处理微秒精度的唯一方法。
you could cast the result if you want less precision:
Edit following OP comment:
From The Oracle Documentation (11gr1):
It seems you will have to manually use EXTRACT to achieve the desired output:
This is not very elegant but it seems it is the only way to deal with microseconds precision.
to_char() 似乎有固定格式:( 所以 regexp_substr 可能是一个选项,例如:
to_char() seems to have fixed format :( so regexp_substr may be an option, e.g.:
我意识到这根本不聪明,也不是您正在寻找的特殊格式字符串,但这个答案确实有效,因为输出是固定长度的:
它也只是截断秒的小数部分而不是四舍五入,但我假设从你的例子无论如何它们都只是零。
这是更大的尴尬,但我无法抗拒:
I realize it's not clever at all, nor is it the special format string you're looking for, but this answer does work, given that the output is fixed length:
It also just truncs the fractional seconds instead of rounding, but I assume from your example they're all just zeros anyway.
This is an even greater embarrassment, but I couldn't resist:
线程死灵的轻微情况,但是我在搜索如何格式化间隔时遇到了这个问题,所以我认为值得添加此评论。
从 Oracle 文档,添加时间戳间隔会产生时间戳,因此通过添加具有零时间元素的常量时间戳,您可以使用标准 to_char 日期时间的格式元素 ...
但是,如果您的间隔可能大于一天,则会出现问题。 没有会产生 0 的日期的格式元素。“DDD”是一年中的某一天,因此在上面的示例中将为 365,如果间隔大于一天,则为 1 或更多。 不过只要您的间隔时间少于 24 小时就可以。
应该补充一下这是在 11g 上,所以很可能不适用于 OP。
Slight case of thread necromancy, however I came across this question while searching for how to format an interval, so I thought it was worth adding this comment.
From the Oracle documentation, adding a timestamp to an interval results in a timestamp, so by adding a constant timestamp with zero time elements you can then use the standard to_char format elements for datetime ...
However, there is an issue if you intervals could be greater than a day. There is no format element for days that will yield 0. "DDD" is day of the year, so would be 365 in the example above, or 1 or more if the interval was greater then a day. This is fine as long as your intervals are less than 24 hours though.
Should add this is on 11g so may well not have be applicable to the OP.
只需添加日期并使用 to_char ('HH24:MI') !
Just add date and use to_char ('HH24:MI') !
您可以使用正则表达式删除最后一部分(或任何部分)
Oracle REGEXP_REPLACE 就是这样做的。
从 table1 中选择 REGEXP_REPLACE( TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))), '..*')
You can strip out the last part (or any part) with regular expression
Oracle REGEXP_REPLACE does just that.
select REGEXP_REPLACE( TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))), '..*') from table1