ORACLE隱式類型轉換

来源:https://www.cnblogs.com/kerrycode/archive/2020/02/16/12316463.html
-Advertisement-
Play Games

隱式類型轉換簡介 通常ORACLE資料庫存在顯式類型轉換(Explicit Datatype Conversion)和隱式類型轉換(Implicit Datatype Conversion)兩種類型轉換方式。如果進行比較或運算的兩個值的數據類型不同時(源數據的類型與目標數據的類型),而且此時又沒有轉... ...


 

隱式類型轉換簡介

 

通常ORACLE資料庫存在顯式類型轉換(Explicit Datatype Conversion和隱式類型轉換(Implicit Datatype Conversion)兩種類型轉換方式。如果進行比較或運算的兩個值的數據類型不同時(源數據的類型與目標數據的類型),而且此時又沒有轉換函數時,那麼ORACLE必須將其中一個值進行類型轉換,使其能夠運算。這就是所謂的隱式類型轉換。其中隱式類型轉換是自動進行的,當然,只有在這種轉換是有意義的時候,才會自動進行。

 

Data Conversion

Generally an expression cannot contain values of different datatypes. For example, an expression cannot multiply 5 by 10 and then add 'JAMES'. However, Oracle supports both implicit and explicit conversion of values from one datatype to another.

 

 

關於隱式類型轉換,建議翻看官方文檔Data Type Comparison Rules章節,下麵是官方文檔中的隱式類型轉換矩陣。從下麵這個表格,我們就能對哪些數據類型能進行轉換一目瞭然。

 

clip_image001[4]

 

 

 

 

隱式轉換的規則:

 

 

其實隱式類型轉換髮生在很多地方,只是我們很多時候沒有留意罷了,不打算一一舉例,自行翻閱官方文檔的介紹,摘抄隱式類型轉換的一些常見的規則如下:

 

The following rules govern implicit data type conversions:

  • During INSERT and UPDATE operations, Oracle converts the value to the data type of the affected column.
  • During SELECT FROM operations, Oracle converts the data from the column to the type of the target variable.
  • When manipulating numeric values, Oracle usually adjusts precision and scale to allow for maximum capacity. In such cases, the numeric data type resulting from such operations can differ from the numeric data type found in the underlying tables.
  • When comparing a character value with a numeric value, Oracle converts the character data to a numeric value.
  • Conversions between character values or NUMBER values and floating-point number values can be inexact, because the character types and NUMBER use decimal precision to represent the numeric value, and the floating-point numbers use binary precision.
  • When converting a CLOB value into a character data type such as VARCHAR2, or converting BLOB to RAW data, if the data to be converted is larger than the target data type, then the database returns an error.
  • During conversion from a timestamp value to a DATE value, the fractional seconds portion of the timestamp value is truncated. This behavior differs from earlier releases of Oracle Database, when the fractional seconds portion of the timestamp value was rounded.
  • Conversions from BINARY_FLOAT to BINARY_DOUBLE are exact.
  • Conversions from BINARY_DOUBLE to BINARY_FLOAT are inexact if the BINARY_DOUBLE value uses more bits of precision that supported by the BINARY_FLOAT.
  • When comparing a character value with a DATE value, Oracle converts the character data to DATE.
  • When you use a SQL function or operator with an argument of a data type other than the one it accepts, Oracle converts the argument to the accepted data type.
  • When making assignments, Oracle converts the value on the right side of the equal sign (=) to the data type of the target of the assignment on the left side.
  • During concatenation operations, Oracle converts from noncharacter data types to CHAR or NCHAR.
  • During arithmetic operations on and comparisons between character and noncharacter data types, Oracle converts from any character data type to a numeric, date, or rowid, as appropriate. In arithmetic operations between CHAR/VARCHAR2 and NCHAR/NVARCHAR2, Oracle converts to a NUMBER.
  • Most SQL character functions are enabled to accept CLOBs as parameters, and Oracle performs implicit conversions between CLOB and character types. Therefore, functions that are not yet enabled for CLOBs can accept CLOBs through implicit conversion. In such cases, Oracle converts the CLOBs to CHAR or VARCHAR2 before the function is invoked. If the CLOB is larger than 4000 bytes, then Oracle converts only the first 4000 bytes to CHAR.
  • When converting RAW or LONG RAW data to or from character data, the binary data is represented in hexadecimal form, with one hexadecimal character representing every four bits of RAW data. Refer to "RAW and LONG RAW Data Types" for more information.
  • Comparisons between CHAR and VARCHAR2 and between NCHAR and 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. Table 2-9 shows the direction of implicit conversions between different character types.

 

對上面官方文檔資料的翻譯如下,如有不對或不夠確切的地方,敬請指出

 

1.  對於INSERTUPDATE操作,ORACLE會把插入值或者更新值隱式轉換為對應欄位的數據類型。

 

2.  對於SELECT語句,ORACLE會把欄位的數據類型隱式轉換為變數的數據類型。

 

3.  當處理數值時,ORACLE通常會調整精度和小數位,以實現最大容量。在這種情況下,由此類操作產生的數字數據類型可能與在基礎表中找到的數字數據類型不同。

 

4.  當比較一個字元型和數值型的值時,ORACLE會把字元型的值隱式轉換為數值型。

 

5.  字元值或NUMBER值與浮點數值之間的轉換可能不准確,因為字元類型和NUMBER使用十進位精度表示數字值,而浮點數則使用二進位精度。

 

6.  CLOB值轉換為字元數據類型(例如VARCHAR2)或將BLOB轉換為RAW數據時,如果要轉換的數據大於目標數據類型,則資料庫將返回錯誤。

 

7.   timestamp類型轉換為DATE時(按照第三條,隱式轉換不應該把timestamp轉換為date,除非insert這樣的),timestamp後幾位會被truncated忽略,至於忽略幾位,取決於資料庫版本。

 

8.  BINARY_FLOATBINARY_DOUBLE的轉換是準確的。

 

9.  BINARY_DOUBLEBINARY_FLOAT的轉換是不精確的,因為BINARY_DOUBLE精度更高。

 

10.  當比較字元型和日期型的數據時,ORACLE會把字元型轉換為日期型。

 

11. 如果調用函數(過程)或運算符操作時,如果輸入參數的數據類型與函數(存儲過程)定義的參數數據類型不一致或不是可接受的數據類型時,則ORACLE會把輸入參數的數據類型轉換為函數或者過程定義的數據類型。

 

12. 當使用賦值符號(等號)時,右邊的類型轉換為左邊的類型

 

13. 當連接操作(concatenation,一般為||)時,ORACLE會隱式轉換非字元型到字元型

 

14. 如果字元類型的數據和非字元類型的數據(numberdaterowid)作算術運算,則ORACLE會將字元類型的數據轉換為合適的數據類型,這些數據類型可能是numberdaterowid等。

  如果CHAR/VARCHAR2 NCHAR/NVARCHAR2之間作算術運算,則ORACLE會將她們都轉換為number類型的數據再做比較。

 

 

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

 

 

下麵簡單舉兩個例子,看看隱式轉換髮生的場景:

 

例子:

 

SQL> create table test(object_id varchar2(12), object_name varchar2(64));
 
Table created.
 
SQL> insert into test
  2  select object_id, object_name from dba_objects;
 
63426 rows created.
 
SQL> commit;
 
	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • rpm可以實現程式的快速,簡單安裝(跟編譯安裝比),但是rpm自己不能解決依賴,所以很多工具為了自動解決依賴應運而生,其中yum就是其中之一。 yum解決依賴的辦法: 必須有個文件伺服器,裡面放置所以的rpm包,以及包的相關元數據( qi, qR得到的數據)。元數據可以使用 來創建。並放置在repo ...
  • 1 DHCP伺服器簡介 DHCP(Dynamic Host Configuration Protocol),動態主機配置協議,DHCP 協議主要是用來自動為區域網中的客戶機分配TCP/IP 信息的網路協議,並完成每台客戶機的TCP/IP 協議配置。當我們將區域網中客戶機IP地址設置為動態獲取方式時, ...
  • 實驗環境:centos7 註:因為本次實驗在同一臺伺服器上,Apache與Nginx同為80埠,所以改Apache埠為60 1 配置Nginx伺服器: 編輯Nginx配置文件,寫入以下內容 location ~ \.php$ { 所有以.php結尾的文件,前面\代表轉義 proxy_pass h ...
  • 很多人都有寫博客的習慣,奈何國內的博客網站正在一家家地關閉與重整,部分博客網站也充斥著太多的廣告,使用體驗非常不好。對於愛寫博客的朋友來說,其實還有一個更好的選擇,那就是自己搭建一個博客。 ...
  • 前言 年過30惶惶不安,又逢疫情,還是不斷學習,強化自己的能力。hadoop的視頻和書籍在15年的時候就看過,但是一直沒動手實踐過,要知道技術不經過實戰,一點提升也沒有。因此下定決心邊學邊做,希望能有所收穫。 軟體版本介紹 virtualbox 6.1 centos7 hadoop 3.2.1 jd ...
  • 1 查詢指定欄位 在 employee 表找出所有員工的姓名、性別和電子郵箱。 SELECT 表示查詢,隨後列出需要返回的欄位,欄位間逗號分隔 FROM 表示要從哪個表中進行查詢 分號為語句結束符 這種查詢表中指定欄位的操作在關係運算中被稱為投影(Projection) 使用 SELECT 子句進行 ...
  • 1、概述 (1)鎖的定義 鎖是電腦協調多個進程或線程併發訪問某一資源的機制。 在資料庫中,除了傳統的計算資源(如CPU、RAM、IO等)的爭用以外,數據也是一種供需要用戶共用的資源。如何保證數據併發訪問的一致性、有效性是所有資料庫必須解決的一個問題,鎖衝突也是影響資料庫併發訪問性能的一個重要因素。 ...
  • 該文為《 MySQL 實戰 45 講》的學習筆記,感謝查看,如有錯誤,歡迎指正 一、查詢和更新上的區別 這兩類索引在查詢能力上是沒差別的,主要考慮的是對更新性能的影響。建議儘量選擇普通索引。 1.1 MySQL 的查詢操作 普通索引 查找到第一個滿足條件的記錄後,繼續向後遍歷,直到第一個不滿足條件的 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...