如何从Oracle SQL Developer中的DateTime字段中提取时间? [英] How to extract only Time from a DateTime field in Oracle SQL Developer?

查看:485
本文介绍了如何从Oracle SQL Developer中的DateTime字段中提取时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过 To_Timestamp 和SQL Developer的其他方法,但只有这一个对我有用。

 选择To_Number(To_Char(DateTime_FieldName,'HH24'))
|| ':'|| to_number(to_char(DateTime_FieldName,'MI'))
|| ':'|| to_number(to_char(DateTime_FieldName,'SS'))
from TABLE_NAME

是否有更好的解决方案?

解决方案

假设你的目标是生成一个代表时间的字符串(这是什么查询您发布的回报,尽管无关紧要的 to_number 调用)

  SELECT to_char << column_name>>'HH24:MI:SS')
FROM table_name

如果要返回不同的数据类型,您需要告诉我们您要返回的数据类型。例如,如果您真的要返回一个 INTERVAL DAY TO SECOND

  SELECT numtodsinterval(< column name>>  -  trunc(< column name>>),'day')
FROM table_name
/ pre>

I tried To_Timestamp and other methods for SQL Developer but only this one worked fine for me.

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?

解决方案

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)

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

SELECT numtodsinterval( <<column name>> - trunc(<<column name>>), 'day' )
  FROM table_name

这篇关于如何从Oracle SQL Developer中的DateTime字段中提取时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆