2017-09-24 74 views
0

中的數據類型時轉換失敗我收到錯誤「錯誤:將varchar值'10.8'轉換爲數據類型時出錯」。錯誤:在將varchar值'10.8'轉換爲

我正在Azure數據倉庫中寫入此查詢。 什麼可能是錯的,我沒有做任何轉換爲​​Int。

select 
    cast(
    case 
     when [total_amount] is null then 0 
     when [total_amount] = '' then 0 
     else [total_amount] 
    end 
    as decimal(10,4) 
) 
    FROM [dbo].[ABC] 

這個查詢是一個外部表查詢其也報告錯誤在查詢執行計劃步驟2,從外部表[NYCTaxiData]拒絕 6行:

Location: '/2016/yellow_tripdata_2016-07.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
    Location: '/2016/yellow_tripdata_2016-10.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
    Location: '/2016/yellow_tripdata_2016-11.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
    Location: '/2016/yellow_tripdata_2016-09.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
    Location: '/2016/yellow_tripdata_2016-08.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
    Location: '/2016/yellow_tripdata_2016-12.csv' Column ordinal: 17, Expected data type: VARCHAR(50) collate SQL_Latin1_General_CP1_CI_AS, Offending value: VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount (Tokenization failed), Error: Not enough columns in this line. 
+0

什麼** datatype **是你的'total_amount'列? –

+0

它是varchar(50)NULL –

回答

2

在這裏你去:

DECLARE @N VARCHAR(MAX) = '10.8'; 

SELECT total_amount = CASE 
         WHEN @N IS NULL THEN 0 
         WHEN @N = '' then 0 
         ELSE 
         CAST(@N AS DECIMAL(10,4)) 
END 

原始代碼的問題是CASE表達式的數據類型爲INT

這是因爲它有兩個分支返回一個整數常量0和一個分支返回varcharint具有higher datatype precedencevarchar

因此,它首先試圖隱含地將字符串'10.8'作爲int並失敗。

隨着上面的改寫varchar分支現在變爲decimal(10,4)。這比int更高的優先級,CASE表達式的數據類型現在成爲decimal(10,4),並且沒有問題。

結果:

+==============+ 
| total_amount | 
+==============+ 
|  10,8000 | 
+--------------+ 

Demo