如何仅从 Oracle SQL Developer 的 DateTime 字段中提取时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17104414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract only Time from a DateTime field in Oracle SQL Developer?
提问by MontyPython
I tried To_Timestamp
and other methods for SQL Developer but only this one worked fine for me.
我尝试To_Timestamp
了 SQL Developer 的其他方法,但只有这个方法对我有用。
Select To_Number(To_Char(DateTime_FieldName, 'HH24'))
|| ':' || to_number(to_char(DateTime_FieldName, 'MI'))
|| ':' ||to_number(to_char(DateTime_FieldName, 'SS'))
from TABLE_NAME
Is there a better solution to this?
有没有更好的解决方案?
回答by Justin Cave
Assuming your goal is to generate a string representing the time (which is what the query you posted returns despite the extraneous to_number
calls)
假设您的目标是生成一个表示时间的字符串(这是您发布的查询返回的内容,尽管有无关的to_number
调用)
SELECT to_char( <<column_name>>, 'HH24:MI:SS' )
FROM table_name
If you want to return a different data type, you'd need to tell us what data type you want to return. If, for example, you really want to return an INTERVAL DAY TO SECOND
如果要返回不同的数据类型,则需要告诉我们要返回的数据类型。例如,如果您真的想返回一个INTERVAL DAY TO SECOND
SELECT numtodsinterval( <<column name>> - trunc(<<column name>>), 'day' )
FROM table_name