Varchar 到数值的转换
Varchar to Numeric Conversion
我已经使用 SSIS 将 excel 数据加载到 table 中。
表结构:
Monthly_Budget
SEQUENCE INT IDENTITY,
TRANSACTION_DATE VARCHAR(100),
TRANSACTION_REMARKS VARCHAR(1000),
WITHDRAWL_AMOUNT VARCHAR(100),
DEPOSIT_AMOUNT VARCHAR(100),
BALANCE_AMOUNT VARCHAR(100)
WITHDRAWL_AMOUNT 列中的值:
7,987.00
1,500.00
7,000.00
50.00
NULL
253.00
4,700.00
2,000.00
148.00
2,000.00
64.00
1,081.00
2,000.00
NULL
NULL
7,000.00
现在,我正在尝试运行查询以获取 WITHDRWAL_AMOUNT 下的值的总和,但出现错误:
Error converting data type varchar to numeric.
我的查询:
SELECT SUM(CAST(ISNULL(LTRIM(RTRIM(WITHDRAWL_AMOUNT)),0) AS NUMERIC(6,2))) AS NUM FROM MONTHLY_BUDGET
尝试像这样转换它们:
select SUM(CAST(ltrim(rtrim(replace(WITHDRAWL_AMOUNT, ',', ''))) as numeric(6, 2)) )
将值存储在您想要的适当类型中是非常非常可取的。但是,我可以理解将外部数据放入暂存 table,然后使用上述逻辑将数据加载到最终 table.
我已经使用 SSIS 将 excel 数据加载到 table 中。
表结构:
Monthly_Budget
SEQUENCE INT IDENTITY,
TRANSACTION_DATE VARCHAR(100),
TRANSACTION_REMARKS VARCHAR(1000),
WITHDRAWL_AMOUNT VARCHAR(100),
DEPOSIT_AMOUNT VARCHAR(100),
BALANCE_AMOUNT VARCHAR(100)
WITHDRAWL_AMOUNT 列中的值:
7,987.00
1,500.00
7,000.00
50.00
NULL
253.00
4,700.00
2,000.00
148.00
2,000.00
64.00
1,081.00
2,000.00
NULL
NULL
7,000.00
现在,我正在尝试运行查询以获取 WITHDRWAL_AMOUNT 下的值的总和,但出现错误:
Error converting data type varchar to numeric.
我的查询:
SELECT SUM(CAST(ISNULL(LTRIM(RTRIM(WITHDRAWL_AMOUNT)),0) AS NUMERIC(6,2))) AS NUM FROM MONTHLY_BUDGET
尝试像这样转换它们:
select SUM(CAST(ltrim(rtrim(replace(WITHDRAWL_AMOUNT, ',', ''))) as numeric(6, 2)) )
将值存储在您想要的适当类型中是非常非常可取的。但是,我可以理解将外部数据放入暂存 table,然后使用上述逻辑将数据加载到最终 table.