Error: Conversion failed when converting the varchar value '10.8' to data type in
Error: Conversion failed when converting the varchar value '10.8' to data type in
我收到错误 "Error: Conversion failed when converting the varchar value '10.8' to data type in."。
我正在 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]
此查询是一个外部 table 查询,它也报告错误
6 行在查询执行的计划步骤 2 中被外部 table [NYCTaxiData] 拒绝:
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.
给你:
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
和一个返回 varchar
和 int
的分支 higher datatype precedence 而不是 varchar
。
所以它首先尝试将字符串 '10.8'
隐式转换为 int
但失败了。
通过上面的重写,varchar
分支现在变为 decimal(10,4)
。这比int
的优先级更高,CASE
表达式的数据类型现在整体变成了decimal(10,4)
,没有问题。
结果:
+==============+
| total_amount |
+==============+
| 10,8000 |
+--------------+
Demo.
我收到错误 "Error: Conversion failed when converting the varchar value '10.8' to data type in."。
我正在 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]
此查询是一个外部 table 查询,它也报告错误 6 行在查询执行的计划步骤 2 中被外部 table [NYCTaxiData] 拒绝:
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.
给你:
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
和一个返回 varchar
和 int
的分支 higher datatype precedence 而不是 varchar
。
所以它首先尝试将字符串 '10.8'
隐式转换为 int
但失败了。
通过上面的重写,varchar
分支现在变为 decimal(10,4)
。这比int
的优先级更高,CASE
表达式的数据类型现在整体变成了decimal(10,4)
,没有问题。
结果:
+==============+
| total_amount |
+==============+
| 10,8000 |
+--------------+
Demo.