2017-09-24 53 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