oracle資料隱式轉換規則

space6212發表於2019-07-22

在oracle中,如果不同的資料型別之間關聯,如果不顯式轉換資料,則它會根據以下規則對資料進行隱式轉換


The following rules govern the direction in which Oracle makes implicit datatype conversions:

1) During INSERT and UPDATE operations, Oracle converts the value to the datatype of the affected column.
對於INSERT和UPDATE操作,oracle會把插入值或者更新值隱式轉換為欄位的資料型別。如
假如id列的資料型別為number
update t set id='1'; -> 相當於 update t set id=to_number('1');
insert into t(id) values('1') -> insert into t values(to_number('1'));

2) During SELECT FROM operations, Oracle converts the data from the column to the type of the target variable.
對於SELECT語句,oracle會把欄位的資料型別隱式轉換為變數的資料型別。如
假設id列的資料型別為varchar2
select * from t where id=1; -> select * from t where to_number(id)=1;
但如果id列的資料型別為number,則
select * from t where id='1'; -> select * from t where id=to_number('1');(參考下文)

3) When comparing a character value with a NUMBER value, Oracle converts the character data to NUMBER.
當比較一個字元型和數值型的值時,oracle會把字元型的值隱式轉換為數值型。如
假設id列的資料型別為number
select * from t where id='1'; -> select * from t where id=to_number('1');

4) When comparing a character value with a DATE value, Oracle converts the character data to DATE.
當比較字元型和日期型的資料時,oracle會把字元型轉換為日期型。如
假設create_date為字元型,
select * from t where create_date>sysdate; -> select * from t where to_date(create_date)>sysdate;(注意,此時session的nls_date_format需要與字串格式相符)
假設create_date為date型,
select * from t where create_date>'2006-11-11 11:11:11'; -> select * from t where create_date>to_date('2006-11-11 11:11:11'); (注意,此時session的nls_date_format需要與字串格式相符)

5) When you use a SQL function or operator with an argument of a datatype other than the one it accepts,
Oracle converts the argument to the accepted datatype.
如果呼叫函式或過程等時,如果輸入引數的資料型別與函式或者過程定義的引數資料型別不一直,則oracle會把輸入引數的資料型別轉換為函式或者過程定義的資料型別。如
假設過程如下定義
p(p_1 number)
exec p('1'); -> exec p(to_number('1'));

6) When making assignments, Oracle converts the value on the right side of the equal sign (=) to the datatype of the target of the assignment on the left side.
賦值時,oracle會把等號右邊的資料型別轉換為左邊的資料型別。如
var a number
a:='1'; - > a:=to_number('1');


7) During concatenation operations, Oracle converts from noncharacter datatypes to CHAR or NCHAR.
用連線運算子(||)時,oracle會把非字元型別的資料轉換為字元型別。
select 1||'2' from dual; -> select to_char(1)||'2' from dual;

8) During arithmetic operations on and comparisons between character and noncharacter datatypes,
Oracle converts from any character datatype to a number, date, or rowid, as appropriate.
In arithmetic operations between CHAR/VARCHAR2 and NCHAR/NVARCHAR2, Oracle converts to a number.
如果字元型別的資料和非字元型別的資料(如number、date、rowid等)作算術運算,則oracle會將字元型別的資料轉換為合適的資料型別,這些資料型別可能是number、date、rowid等。
如果CHAR/VARCHAR2 和NCHAR/NVARCHAR2之間作算術運算,則oracle會將她們都轉換為number型別的資料再做比較。

9) Comparisons between CHAR/VARCHAR2 and NCHAR/NVARCHAR2 types may entail different character sets.
The default direction of conversion in such cases is from the database character set to the national character set.

比較CHAR/VARCHAR2 和NCHAR/NVARCHAR2時,如果兩者字符集不一樣,則預設的轉換方式是將資料編碼從資料庫字符集轉換為國家字符集。


簡單總結:
比較時,一般是字元型轉換為數值型,字元型轉換為日期型
算術運算時,一般把字元型轉換為數值型,字元型轉換為日期型
連線時(||),一般是把數值型轉換為字元型,日期型轉換為字元型
賦值、呼叫函式時,以定義的變數型別為準。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/231499/viewspace-63725/,如需轉載,請註明出處,否則將追究法律責任。

相關文章