截断时间戳
假设我有一个时间戳变量:
select timestamp '2011-02-24 08:30:42 +06:00' from dual;
有什么方法可以将其“截断”为类似的内容
'2011-02-24 08:00:00 +06:00'
(我已经削减了分钟和秒,但保留了时区)
目标oracle版本是11g r2
Let's suppose I have a timestamp variable:
select timestamp '2011-02-24 08:30:42 +06:00' from dual;
Is there any way to "truncate" it to something like
'2011-02-24 08:00:00 +06:00'
(I've cut minutes and seconds, but left the timezone)
The target oracle version is 11g r2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我让它工作使用:
TRUNC 支持 时间戳 9.2.0.3+,但在我对 10g Express Edition 的测试中,这
...完全搞砸了时间和上午/下午。对于文字来说并不重要,或者如果在 TO_TIMESTAMP_TZ 内提供的话对我来说并不重要。 10gR2 的 TRUNC(date) 文档表示“返回的值始终是 DATE 数据类型,即使您为 date 指定了不同的日期时间数据类型。”。
I got it to work using:
TRUNC supports timestamps 9.2.0.3+, but in my testing on 10g Express Edition this
...totally screwed the hours and AM/PM. Didn't matter for a literal, or if supplied inside of TO_TIMESTAMP_TZ for me. The documentation for TRUNC(date) for 10gR2 says "The value returned is always of datatype DATE, even if you specify a different datetime datatype for date.".
您的 NLS_DATE_FORMAT 可能设置为仅显示日期部分。这对我有用:
请记住,返回的结果是日期,因此您会丢失任何本地化信息。您可以通过将结果转换为时间戳来返回 GMT 标准化时间戳:
要保留时区信息,您必须执行一些非常复杂的操作。此时使用存储函数来执行转换可能会更容易。这与 Oracle 内置 TRUNC() 函数非常相似:
我创建了一个 sqlfiddle 用于演示。
Your NLS_DATE_FORMAT is probably set to display only the date portion. This works for me:
Keep in mind that the result returned is a DATE, so you lose any localization info. You can return a GMT-normalized timestamp by casting the result to a timestamp:
To preserve the timezone info you have to do something pretty convoluted. It might be easier to use a stored function to perform the conversion at that point. This works much like the Oracle built-in TRUNC() function:
I created a sqlfiddle for a demo.
尝试
从双重中选择演员(时间戳为日期)
Try
select cast(timestamp as date) from dual