Oracle SQL“从日期时间字段中选择日期”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29004960/
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
Oracle SQL "SELECT DATE from DATETIME field "
提问by Dejan
I have field REPORTDATE (DATETIME). In SQL Developer i can see its value in this format
我有字段 REPORTDATE (DATETIME)。在 SQL Developer 中,我可以以这种格式查看它的值
29.10.2013 17:08:08
29.10.2013 17:08:08
I found that in order to do the select of just a DATE I need to execute this:
我发现为了只选择一个 DATE,我需要执行这个:
SELECT TO_DATE (REPORTDATE, 'DD.MON.YYYY') AS my_date
FROM TABLE1
but it returns 0RA-01843: not a valid month
但它返回 0RA-01843: not a valid month
I want result to return only 29.10.2013
我希望结果只返回 29.10.2013
回答by Lalit Kumar B
TO_DATE (REPORTDATE, 'DD.MON.YYYY')
TO_DATE(报告日期,'DD.MON.YYYY')
This makes no sense. You are converting a date into a date again. You use TO_DATEto convert a string literal into DATE.
这没有任何意义。您再次将日期转换为日期。您使用TO_DATE将字符串文字转换为DATE。
I want result to return only 29.10.2013
我希望结果只返回 29.10.2013
You could use TRUNCto truncate the time element. If you want to use this value for DATE calculations, you could use it directly.
您可以使用TRUNC截断时间元素。如果要将此值用于 DATE 计算,则可以直接使用它。
For example,
例如,
SQL> select TRUNC(SYSDATE) dt FROM DUAL;
DT
---------
12-MAR-15
To display in a particular format, you could use TO_CHARand proper FORMAT MASK.
要以特定格式显示,您可以使用TO_CHAR和适当的FORMAT MASK。
SQL> SELECT to_char(SYSDATE, 'DD.MM.YYYY') dt from dual;
DT
----------
12.03.2015
SQL>
回答by Gergely Bacso
Use this:
用这个:
SELECT trunc(REPORTDATE, 'DD') AS my_date
FROM TABLE1
This will not change the type of the returning object, only truncates everything below "day" level.
这不会改变返回对象的类型,只会截断“天”级别以下的所有内容。
If you are ok with returning a String, then you can just do:
如果你可以返回一个字符串,那么你可以这样做:
SELECT TO_CHAR(REPORTDATE, 'DD.MM.YYYY') AS my_date
FROM TABLE1
回答by Majorkumar
Try this code snipppet:
试试这个代码片段:
cast(datetimevariable as date)