gpt4 book ai didi

firebird - 如何在 Firebird DB 中将日期时间格式化/转换为 ISO8601?

转载 作者:行者123 更新时间:2023-12-02 20:25:23 25 4
gpt4 key购买 nike

如何将日期时间格式化为 ISO 8601?

据我所知,Firebird DB 中没有内置函数可以将日期或时间戳格式化为 yyyy-MM-ddTHH:mm:ss.fffff(ISO8601 标准表示形式)。

最佳答案

Firebird 没有内置函数来执行此操作。并且转换为字符串将始终应用特定于区域设置的格式。现在最好的方法是根本不在数据库中执行此操作,而是将其留给应用程序中的表示层。

但是,如果您确实在数据库中需要它,那么稳定的方法是:

extract(year from ts) || 
'-' || lpad(extract(month from ts), 2, '0') ||
'-' || lpad(extract(day from ts), 2, '0') ||
'T' || lpad(extract(hour from ts), 2, '0') ||
':' || lpad(extract(minute from ts), 2, '0') ||
':' || lpad(extract(second from ts), 2, '0')

如果您使用 Firebird 3,那么您可以将其包装在您自己的函数中:

create function iso8601timestamp(ts timestamp) returns varchar(20)
as
begin
return extract(year from ts) ||
'-' || lpad(extract(month from ts), 2, '0') ||
'-' || lpad(extract(day from ts), 2, '0') ||
'T' || lpad(extract(hour from ts), 2, '0') ||
':' || lpad(extract(minute from ts), 2, '0') ||
':' || lpad(extract(second from ts), 2, '0');
end

请注意,我省略了秒的小数部分,因为它们会带来额外的麻烦。

关于firebird - 如何在 Firebird DB 中将日期时间格式化/转换为 ISO8601?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50112996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com