我有一个表“Table1_201403”,其中1个varchar列为'Column 0'。现在,我想通过下面的查询将此表中的必要数据插入到Master_Table2中 -
SELECT '2014/03' AS Period
,ltrim(rtrim(SUBSTRING([Column 0], 1, 12))) AS 'Account'
,cast(ltrim(rtrim(SUBSTRING([Column 0], 17, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 15, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 13, 2))) AS DATE) AS Trans_Date
,CASE
WHEN ltrim(rtrim(SUBSTRING([Column 0], 30, 1))) = '-'
THEN CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))) AS DECIMAL) / 100) AS DECIMAL(9, 2)) * - 1
ELSE CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))) AS DECIMAL) / 100) AS DECIMAL(9, 2))
END AS Amount
INTO Master_Table2
FROM Table1_201403
WHERE SUBSTRING([Column 0], 11, 1) <> 'B'
当我尝试这样做时,我收到错误'错误将数据类型varchar转换为数字'。
什么是合适的解决方案。任何帮助将不胜感激。
感谢。
修改 -
SELECT '2014/03' AS Period
,ltrim(rtrim(SUBSTRING([Column 0], 1, 12))) AS 'GL Account'
,cast(ltrim(rtrim(SUBSTRING([Column 0], 17, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 15, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 13, 2))) AS DATE) AS Trans_Date
,case
when ltrim(rtrim(SUBSTRING([Column 0], 30, 1))) = '-'
THEN(
case when isnumeric(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))))=1 then
CAST(CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) AS decimal)*-1
else
CAST(CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) AS varchar)*-1 end
)
ELSE(
case when isnumeric(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))))=1 then
CAST(CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) AS decimal)
else
CAST(CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) AS varchar) end
)
END as Amount
INTO TABLE2
FROM TABLE1
WHERE SUBSTRING([Column 0], 11, 1) <> 'B'
我尝试使用IsNumeric Logic。但无法使代码正常工作。请看一看。 @Adi T
并且,数据如下所示使用您的脚本@t_m -
第0列ValidDecimal 006125 01031400000451745+ JNLCIS批次01/03/2014 N BA0119 C L 1 006125 01031400000212077+ JNLCIS批次01/03/2014 N BA0120 C L 1 006125 01031400000311569+ JNLCIS批次01/03/2014 N BA0119 P L 1
答案 0 :(得分:1)
怀疑Amount逻辑可能返回一些varchar值而不是decimal。
运行下面的脚本以检查您的金额逻辑是否正确。当您运行脚本时,如果一切正常,则不应返回任何记录。
select *
from
(
select '2014/03' as Period,
ltrim(rtrim(SUBSTRING([Column 0],1,12))) as 'Account',
cast(ltrim(rtrim(SUBSTRING([Column 0],17,2)))+ltrim(rtrim(SUBSTRING([Column 0],15,2)))+ltrim(rtrim(SUBSTRING([Column 0],13,2))) AS DATE)
as Trans_Date,
ISNUMERIC(case when ltrim(rtrim(SUBSTRING([Column 0],30,1)))='-' then CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2))*-1
else CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) END) As ValidDecimal
from Table1_201403
where SUBSTRING([Column 0],11,1) <> 'B'
) CheckForValidDecimal
where ValidDecimal = 0
<强>修改:强>
在下面运行此脚本以查找不正确的[Column 0]记录。然后,您可以更改金额逻辑以解决问题。
select *
from
(
select
[Column 0],
ISNUMERIC(case when ltrim(rtrim(SUBSTRING([Column 0],30,1)))='-' then CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2))*-1
else CAST((CAST(ltrim(rtrim(SUBSTRING([Column 0],19,11))) AS decimal) / 100) AS decimal(9,2)) END) As ValidDecimal
from Table1_201403
where SUBSTRING([Column 0],11,1) <> 'B'
) CheckForValidDecimal
where ValidDecimal = 0
答案 1 :(得分:0)
您正在尝试将数据从varchar转换为decimal。如果数据属于charachters,则无法转换为十进制。
use cast() or convert() function
答案 2 :(得分:0)
请尝试这种方式
SELECT '2014/03' AS Period
,ltrim(rtrim(SUBSTRING([Column 0], 1, 12))) AS 'Account'
,cast(ltrim(rtrim(SUBSTRING([Column 0], 17, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 15, 2))) + ltrim(rtrim(SUBSTRING([Column 0], 13, 2))) AS DATE) AS Trans_Date
,CASE
WHEN ltrim(rtrim(SUBSTRING([Column 0], 30, 1))) = '-'
THEN case when isnumeric( case when isnumeric(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))
))=1 then
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS decimal) else
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS varchar) end)=1
then CAST(case when isnumeric(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))
))=1 then
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS decimal) else
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS varchar) end AS decimal(9,2)) *-1
else
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS varchar)
ELSE
CAST(case when isnumeric(ltrim(rtrim(SUBSTRING([Column 0], 19, 11))
))=1 then
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS decimal) else
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS varchar) end AS decimal(9,2)) /100 AS DECIMAL
else
CAST(ltrim(rtrim(SUBSTRING([Column 0], 19, 11)))AS varchar)
END
INTO Master_Table2
FROM Table1_201403
WHERE SUBSTRING([Column 0], 11, 1) <> 'B'