ORACLE中有哪些日期类型

发布时间:2021-08-05 15:33:24 作者:Leah
来源:亿速云 阅读:283

ORACLE中有哪些日期类型,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

数据库中有4中日期类型:


1、DATE:是最早和使用最广的日期类型,使用示例如下。


DATE is the oldest and most widely used data type. Though the name is "DATE", it stores also information about the time. Internally, DATE stores year, month, day, hour, minute and second as explicit values. To get the current timestamp as an instance of DATE, use the SYSDATE SQL function.


SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
SQL> select sysdate, dump(sysdate) as date_bytes from dual;


SYSDATE              DATE_BYTES                           
-------------------- -----------------------------------
2017-11-23 23:41:08  Typ=13 Len=8: 225,7,11,23,23,41,8,0  




2、TIMESTAMP:扩展了DATA类型,包含了分秒和时区。


TIMESTAMP extends DATE by fractional seconds. Internally, time zone information is also contained, but in order to work with time zones, one of the other two data types, TIMESTAMP WITH TIME ZONE or TIMESTAMP WITH LOCAL TIME ZONE must be used. To get the current timestamp as a TIMESTAMP instance, use the LOCALTIMESTAMP SQL function. As the DUMP result below shows, TIMESTAMP stores year, month, day, hour, minute and second similar to DATE. The other bytes are there for fractional seconds and time zone information. TIMESTAMP instances consume more space on disk than DATE instances, which is natural - they contain more information.




SQL> alter session set nls_timestamp_format='YYYY-MM-DD HH24:MI:SS.FF6';
SQL> select localtimestamp, dump(localtimestamp) ts_bytes from dual;


LOCALTIMESTAMP              TS_BYTES                                                               
--------------------------- ---------------------------------------------------------------------
2017-11-24 08:45:41.434175  Typ=187 Len=20: 225,7,11,24,8,45,41,0,24,252,224,25,1,0,3,0,127,1,0,0


3、SYSTIMESTAMP显示时区,SYSTIMESTAMP显示当前数据库时区,CURRENT_TIMESTAMP显示当前会话时区;


TIMESTAMP WITH TIME ZONE allows to explicitly work with time zone information. A time zone can be used to create a TIMESTAMP WITH TIME ZONE instance and it is explicitly contained in the output (use the right format mask). When the output of a TIMESTAMP WITH TIME ZONE value does not contain a time zone, you have incomplete data.
Oracle does not implicitly convert instances of this data type between time zones; this can be done explicitly with the AT TIME ZONE clause. To get the current time as a TIMESTAMP WITH TIME ZONE instance, we can use two functions: SYSTIMESTAMP returns the current time in the database time zone and CURRENT_TIMESTAMP returns it in the current session time zone. The following example uses SYSTIMESTAMP.


SQL> alter session set nls_timestamp_tz_format='YYYY-MM-DD HH24:MI:SS.FF6 TZR';
SQL> select systimestamp as ts_bytes from dual;


SYSTIMESTAMP
----------------------------------
2017-11-23 23:57:04.609608 -08:00


4、本地时区和数据库时区转换。TIMESTAMP隐含使用本地时区,默认使用会话时区插入,存储时默认使用数据库时区。


TIMESTAMP WITH LOCAL TIME ZONE does not expose the time zone information (to the application, it looks like a TIMESTAMP), but it uses tome zones implicitly: An instance is created without time zone information, the session time zone is assumed. Stored instances are normalized to the database time zone. When the data is retrieved, users see it (again) in the session time zone. 
The following example illustrates how TIMESTAMP WITH LOCAL TIME ZONE works: First, a table is created. Then the current time is retrieved and stored into the table ( SYSTIMESTAMP returns TIMESTAMP WITH TIME ZONE, but this is automatically converted to TIMESTAMP WITH LOCAL TIME ZONE ). When the table is then being selected, the returned data changes with the session time zone.


SQL> create table mytimestamps( ts timestamp with local time zone );
SQL> insert into mytimestamps values ( systimestamp );
SQL> alter session set time_zone='Europe/Berlin';
SQL> select * from mytimestamps;


TS
---------------------------------------------------------------------------
2017-11-24 12:55:39.761283


SQL> alter session set time_zone='EST';
SQL> select * from mytimestamps;


TS
---------------------------------------------------------------------------
2017-11-24 06:55:39.761283




Format Masks


Whether we work within an application or a tool like SQL Plus or SQL Developer; whenever we output a DATE or TIMESTAMP instance, we're converting it to VARCHAR2. We can do this explicitly using TO_CHAR or Oracle will do it implcitly. And now NLS format masks kick in - the format mask decides how a DATE is converted to a VARCHAR2 and back. The are defaults for NLS format masks at the database level, we can set them at the session level or we can pass a format mask to the individual TO_CHAR call.
看完上述内容,你们掌握ORACLE中有哪些日期类型的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. MySQL日期类型
  2. Oracle中的日期类型

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle

上一篇:mysql中怎么测试varchar类型最大长度

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》