format interval with to_char(使用 to_char 格式化间隔)
问题描述
遵循 SQL 命令
select TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))) from table1
产生以下格式的结果:+000000000 00:03:01.954000.
produces a result of the format: +000000000 00:03:01.954000.
是否可以在to_char函数中输入特殊格式才能得到格式的结果:+00 00:00:00.000?
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?
推荐答案
我意识到它根本不聪明,也不是您正在寻找的特殊格式字符串,但是鉴于输出是固定的,这个答案确实有效长度:
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:
SELECT SUBSTR(TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))), 1, 1)
|| SUBSTR(TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))), 9, 2)
|| ' '
|| SUBSTR(TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00'))), 12, 12)
FROM table1;
它也只是截断了小数秒而不是四舍五入,但我从你的例子中假设它们无论如何都只是零.
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:
SELECT SUBSTR(REPLACE(TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL('0 00:00:00')))
, '0000000', '')
, 1, 16)
FROM table1;
这篇关于使用 to_char 格式化间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 to_char 格式化间隔


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01