我希望將高精度值從 Oracle 加載到 Snowflake。我首先通過 Python 腳本從 CSV 檔案中的 oracle 中提取值,然后將其加載到 Snowflake DB 中。我發現該值沒有像 oracle 中的值一樣被提取。我發現精度有一些問題。我不確定這是否與 Oracle(版本 19.12.0.0.0)或 Python 腳本(V 3.7)中的問題有關。請注意,此行為適用于大量值,以上是我在加載時需要遵循的設計。
Oracle DB 中的值:86525.200000000006。這在資料型別“數字(28,12)”的列中可用
提取的 CSV 檔案中的值:86525.2
我已經嘗試過的:
#1。將數字轉換為 Char 如下:
選擇 value1 ,cast(value1 作為 varchar2(1000))
從表 1
這里的 Value1 是 '86525.2'
#2。
TO_CHAR(value1,'fm9999999999999999.999999999999'),
這里的值是'86525.2'
#3。TO_CHAR(value1,'fm9999999999999999.000000000000'),
這里的值是'86525.200000000000'
#4。(SUBSTR (value1 , INSTR (value1 , '.',1))) mydecimal,
這里的值是'86525.2'
#5。to_number(regexp_replace(to_char(value1), '^[0-9] .', '')) rexp,
這里的值是'86525.2'
任何幫助將不勝感激。謝謝你。
uj5u.com熱心網友回復:
問題是您使用的默認資料型別NUMBER是 Python float,這會導致精度丟失。
您必須切換到使用Decimal精度更高的資料型別。
如檔案所述,您必須定義一個輸出型別處理程式,該處理程式Decimal在cursor.
例子
使用來自其他答案的表格。
def number_to_decimal(cursor, name, default_type, size, precision, scale):
if default_type == cx_Oracle.DB_TYPE_NUMBER:
return cursor.var(decimal.Decimal, arraysize=cursor.arraysize)
cursor = connection.cursor()
cursor.outputtypehandler = number_to_decimal
cursor.execute("select val from test" )
for val in cursor:
print("Type :", type(val[0]))
print("Values:", val[0])
輸出
Type : <class 'decimal.Decimal'>
Values: 86525.200000000006
uj5u.com熱心網友回復:
值已正確存盤到表中:
樣本資料:
SQL> create table test (val number(28, 12));
Table created.
SQL> insert into test values (86525.200000000006);
1 row created.
內容:
SQL> select * From test;
VAL
----------
86525,2 --> not all decimals here
設定列格式:
SQL> col val format 99990d000000000000
SQL> select * From test;
VAL
-------------------
86525,200000000006 --> correct!
SQL>
to_char 也可以:
SQL> select to_char(val, '99990d000000000000') result from test;
RESULT
-------------------
86525,200000000006
SQL>
uj5u.com熱心網友回復:
你有沒有嘗試過這樣的事情:
from decimal import Decimal
y= decimal(1)/decimal(3)
print(type(y))
print(y)
輸出:
<class 'decimal.Decimal'> 0.333333333333333333333333
你可以把你的價值放進去decimal(value),然后繼續。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408200.html
標籤:
上一篇:將JSON_ARRAY_T轉換為SQL運算式的運算式串列
下一篇:如果表為空,則顯示訊息
