[20230207]ANSI DATE and TIMESTAMP SQL syntax.txt
[20230207]ANSI DATE and TIMESTAMP SQL syntax.txt
--//以前我一直以為oracle僅僅支援ansi date語法,當然我很少使用.我個人比較喜歡直接使用字串.
--//只要NLS*環境變數與字串日期順序一致,一般不會存在任何問題,而且也不存在隱式轉換.
--//看了http://blog.tanelpoder.com/2012/12/29/a-tip-for-lazy-oracle-users-type-less-with-ansi-date-and-timestamp-sql-syntax
--//才發現也支援timestamp的語法.
--//簡單測試看看.
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.2.0.1.0 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0
2.測試例子:
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > DATE'2012-12-01';
COUNT(*)
----------
22529
SCOTT@test01p> select count(*) from dba_objects where created > date'2012-12-01';
COUNT(*)
----------
22529
SCOTT@test01p> select count(*) from dba_objects where created > date '2012-12-01';
COUNT(*)
----------
22529
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > DATE'2012/12/01';
SELECT COUNT(*) FROM dba_objects WHERE created > DATE'2012/12/01'
*
ERROR at line 1:
ORA-01861: literal does not match format string
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > date'2012-01-01 08:12:34';
SELECT COUNT(*) FROM dba_objects WHERE created > date'2012-01-01 08:12:34'
*
ERROR at line 1:
ORA-01861: literal does not match format string
--//DATE'2012-12-01' 表示2012-01-01 00:00:00,注意並不區分大小寫.另外注意分隔符號不能使用/等其它分割符號.
--//date與日期之間可以存在空格.
--//如果有時分秒我以前一直認為不行,實際上前面使用TIMESTAMP代替date.
SCOTT@test01p> select count(*) from dba_objects where created > timestamp'2012-12-01';
select count(*) from dba_objects where created > timestamp'2012-12-01'
*
ERROR at line 1:
ORA-01861: literal does not match format string
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > TIMESTAMP'2012-01-01 08:12:34';
COUNT(*)
----------
22529
SCOTT@test01p> select count(*) from dba_objects where created > timestamp'2012-01-01 08:12:34';
COUNT(*)
----------
22529
SCOTT@test01p> select count(*) from dba_objects where created > timestamp'2012-01-01 08:12:34.00001';
COUNT(*)
----------
22529
--//timestamp與date類似,不同在於timestamp的日期格式要輸入時分秒.date不能輸入時分秒.
--//注意這裡的執行計劃可能存在隱式轉換問題.因為timestamp型別比date型別高.
--//我個人以前喜歡直接使用字串.
d:\tmp> set | grep -i nls
NLS_DATE_FORMAT=YYYY-MM-DD HH24:MI:SS
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
NLS_TIMESTAMP_FORMAT=YYYY-MM-DD HH24:MI:SS.FF
NLS_TIMESTAMP_TZ_FORMAT=YYYY-MM-DD HH24:MI:SS.FF
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > '2012-12-01';
COUNT(*)
----------
22529
SCOTT@test01p> SELECT COUNT(*) FROM dba_objects WHERE created > '2012/12/01';
COUNT(*)
----------
22529
SCOTT@test01p> SELECT COUNT(*) FROM emp WHERE hiredate> '1981:12:01 00/00/01';
COUNT(*)
----------
5
SCOTT@test01p> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 029m424dy0j35, child number 0
-------------------------------------
SELECT COUNT(*) FROM emp WHERE hiredate> '1981:12:01 00/00/01'
Plan hash value: 580533841
-----------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | INDEX SKIP SCAN| E_SORT1 | 5 | 40 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / EMP@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("HIREDATE">TO_DATE(' 1981-12-01 00:00:01', 'syyyy-mm-dd
hh24:mi:ss'))
filter("HIREDATE">TO_DATE(' 1981-12-01 00:00:01', 'syyyy-mm-dd
hh24:mi:ss'))
--//直接使用字串不存在字串轉換問題,分割符號不一定是-,其它也可以.但是不支援timestamp的型別.
SCOTT@test01p> SELECT COUNT(*) FROM emp WHERE hiredate> '1981:12:01 00/00/01.000001';
SELECT COUNT(*) FROM emp WHERE hiredate> '1981:12:01 00/00/01.000001'
*
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2934660/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20111229]date time timestamp? ansi liternals.txt
- date、timestamp欄位型別型別
- java convert timestamp to date and timeJava
- oracle timestamp轉換date及date型別相減Oracle型別
- Oracle中Date和Timestamp的區別Oracle
- oracle plsql定義date_timestamp_with time zoneOracleSQL
- 將timestamp轉成date格式的指令碼指令碼
- date和timestamp型別之間的轉換型別
- String和Date、Timestamp之間的轉換
- JAVA裡的String、Timestamp、Date相互轉換Java
- 將timestamp型別轉換為date型別型別
- mysql資料庫date 、datetime、time、timestamp區別MySql資料庫
- ORACLE DATE型別和TIMESTAMP型別DUMP換算Oracle型別
- oracle資料型別date和timestamp的轉化Oracle資料型別
- Java:String和Date、Timestamp之間的轉換Java
- pl/sql to_dateSQL
- java util date轉換成java sql dateJavaSQL
- Mysql日期(DATE, TIME, DATETIME, TIMESTAMP)型別的比較MySql型別
- ORACLE DATE和TIMESTAMP資料型別的比較(一) (轉)Oracle資料型別
- ORACLE DATE和TIMESTAMP資料型別的比較(二) (轉)Oracle資料型別
- ORA-00001 : Unique Constraint Violated caused by DATE VS Timestamp(6)AI
- Oracle timestamp型別轉換date格式指令碼(精確到秒)Oracle型別指令碼
- SQL Server中timestamp(時間戳)SQLServer時間戳
- 【SQL】17 SQL 檢視(Views)、SQL Date 函式、SQL NULL 值、SQLView函式Null
- 面試題:能談談Date、Datetime、Time、Timestamp、year的區別嗎?面試題
- 【等待事件】SQL*Net vector date to client事件SQLclient
- java.util.Date和java.sql.Date有什麼區別?JavaSQL
- mysql中的date、datetime、timestamp你還不知道怎麼使用嗎MySql
- oracle中date資料型別與timestamp資料型別的轉換Oracle資料型別
- (轉)PLS_INTEGER型別與timestamp型別、date、及時間函式型別函式
- oracle將表中date資料型別修改為timestamp資料型別Oracle資料型別
- python date 和 datetime 的取值範圍(對比 Mysql 的 datetime 和 timestamp)PythonMySql
- oracle 中關於當前的sysdate,sysstimestap,current_date,curent_timestamp 區別Oracle
- SQL Server日期資料型別DATE的使用SQLServer資料型別
- java.lang.IllegalArgumentException at java.sql.Date.getHoursJavaExceptionSQL
- 解決Struts2 json-plugin Date或Timestamp等日期格式帶T的問題JSONPlugin
- sql monitor中timestamp變數轉換可識別格式SQL變數
- Oracle timestamp_to_scn and scn_to_timestampOracle