正确使用Date和Timestamp Oracle数据类型

时间:2017-03-21 09:58:33

标签: sql oracle plsql sql-timestamp

发现在我的脚本中格式化日期和时间戳很困难。

v_date1 Date;
v_date2 timestamp;

假设我有一个created_date字段,值为' 31-03-2017 18:02:05'

select c1 into v_date1 from table_a;
  

给我 - ' 31-03-2017'

select c1 into v_date2 from table_a;
  

给我 - ' 31-03-2017 06:02:05 PM'

我正在尝试通过游标获取数据,从而迭代它以进行少量验证。

如何确保v_date2只考虑日期和时间?

请帮忙。

3 个答案:

答案 0 :(得分:1)

要了解这种差异,首先需要了解会话会话nls设置的工作原理。

SQL> @C:\Users\44011713\Desktop\so_test.sql
SQL> SET SQLBL ON;
SQL> SET ECHO ON;
SQL> SET FEEDBACK ON;
SQL> 
SQL> 
SQL> --To understand this differenence you need to first understand how session nls settings work.
SQL> 
SQL> SELECT * FROM nls_session_parameters
  2  where parameter = 'NLS_DATE_FORMAT';

PARAMETER                                                                       
--------------------------------------------------------------------------------
VALUE                                                                           
--------------------------------------------------------------------------------
NLS_DATE_FORMAT                                                                 
MM/DD/YYYY                                                                      


1 row selected.

SQL> 
SQL> 
SQL> SELECT CURRENT_DATE FROM DUAL;

CURRENT_DA                                                                      
----------                                                                      
03/22/2017                                                                      

1 row selected.

SQL> -- Since we are fetcing date value and the session format mask is MM/DD/YYYY so the output --will be aligning to this format only
SQL> 
SQL> SELECT * FROM nls_session_parameters
  2  where parameter = 'NLS_TIMESTAMP_FORMAT';

PARAMETER                                                                       
--------------------------------------------------------------------------------
VALUE                                                                           
--------------------------------------------------------------------------------
NLS_TIMESTAMP_FORMAT                                                            
MM/DD/YYYY HH24:MI:SS.FF6                                                       


1 row selected.

SQL> 
SQL> 
SQL> ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'MM/DD/YYYY HH24:MI:SS.FF6';

Session altered.

SQL> 
SQL> SELECT LOCALTIMESTAMP FROM DUAL;

LOCALTIMESTAMP                                                                  
---------------------------------------------------------------------------     
03/22/2017 09:53:54.133795                                                      

1 row selected.

SQL> -- Since we are fetcing date value and the session format mask is MM/DD/YYYY --HH24:MI:SS.FF6 so the output will be aligning to this format only
SQL> 
SQL> --Now lets tweak some settings :P
SQL> 
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH24:MI:SS';

Session altered.

SQL> 
SQL> SELECT CURRENT_DATE FROM DUAL;

CURRENT_DATE                                                                    
-------------------                                                             
03/22/2017 09:53:54                                                             

1 row selected.

SQL> -- And now the format aligns to the forced the setting done by ALTER SESSION.
SQL> 
SQL> -- So above example illustrates how the the format ca be changed for session
SQL> 
SQL> --Hope this helps.
SQL> spool off;

答案 1 :(得分:0)

DATE数据类型将存储日期以及时间戳。但是,timestamp数据类型也将存储几分之一秒。

您没有看到DATE数据类型的时间的原因是您的NLS设置。将它们更改为

ALTER SESSION SET nls_date_format='DD-MM-YYYY HH24:MI:SS';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT='DD-MM-RR HH24.MI.SSXFF';

仅当代码中的小部分时间很重要时才使用timestamp。 DATE适用于任何其他用途。

答案 2 :(得分:0)

我认为你误解了日期和时间戳

select c1 into v_date1 from table_a; 

给你一个约会对象

v_date2 timestamp;

为您提供时间戳对象

这两个对象都包含日期和时间组件。时间戳也包含更详细的(第二信息的一部分)。

如果您只想要约会,可以使用许多内置功能...

获取完整信息......

select systimestamp from dual;
select sysdate from dual;

但请注意,您的查询环境可能不会显示完整的日期信息,具体取决于其设置。

删除时间组件......

select trunc(systimestamp) from dual;
select trunc(sysdate) from dual;

或更多控制

select trunc(systimestamp, 'dd') from dual;
select trunc(sysdate, 'dd') from dual;

其中'dd'可以替换为其他值,例如'yyyy',如果你想获得一年的第一天,或者'mm',那就是一个月的第一天。

或舍入而不是删除(再次使用可选值和日期为默认值)

select round(systimestamp, 'dd') from dual;
select round(sysdate, 'dd') from dual;

但是所有这些函数仍然返回数据/时间戳值,因此它们仍然包含时间信息 - 它只是被操纵。

如果您想要的只是一个字符串,那么您需要使用to_char

select to_char(systimestamp, 'dd-mm-yyyy') from dual;
select to_char(sysdate, 'dd-mm-yyyy') from dual;

使用您喜欢的日期格式。