SQL 如何在 Oracle 中将日期转换为日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33921743/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:13:06  来源:igfitidea点击:

How to convert date to datetime in Oracle?

sqloracledatedatetimedate-formatting

提问by Joel Patrick Ndzie

i have a date in oracle with this format DD-MM-YYYand i want to convert it to datetime with this other format DD-MM-YYY HH24:MIhow can i proceed?

我在 oracle 中有一个这种格式的日期,DD-MM-YYY我想用另一种格式将它转换为日期时间,我该DD-MM-YYY HH24:MI如何继续?

I've tried this but nothing is working :

我试过这个,但没有任何效果:

to_date(the_date,'DD-MM-YYY HH24:MI')

and also this:

还有这个:

to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')

回答by Lalit Kumar B

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI

我在 oracle 中有一个日期格式为 DD-MM-YYY,我想将它转换为日期时间格式为 DD-MM-YYY HH24:MI

No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 byteswith each byte storing different components of the datetime value.

不,你糊涂了。Oracle 不会以您看到的格式存储日期它在内部存储7 bytes,每个字节存储日期时间值的不同组成部分。

DATEdata type always has both dateand timeelements up to a precision of seconds.

DATE数据类型始终具有精度高达秒的日期时间元素。

If you want to display, use TO_CHARwith proper FORMAT MODEL.

如果要显示,请使用TO_CHAR和适当的FORMAT MODEL

For example,

例如,

SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42

回答by Tatiana

Oracle DATEdatatype ALWAYS contains (stores) time.

OracleDATE数据类型始终包含(存储)时间。

If you want to see it, you can use function TO_CHAR.

如果你想看到它,你可以使用函数 TO_CHAR。

If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.

如果要添加,例如 1 小时,则可以使用date_debut_p+1/24.

回答by akanksha gupta

If you want to covert to timestamp, you can do the following:

如果要转换为时间戳,可以执行以下操作:

Select to_timestamp(date_column, 'DD-MM-YYY') from table;

从表中选择 to_timestamp(date_column, 'DD-MM-YYY');

However, if you want in the required format, you can do the following:

但是,如果您想要所需的格式,您可以执行以下操作:

Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI') from table;

从表中选择 to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI');

Hope it helps..

希望能帮助到你..