使用联合查询选择数字新列

时间:2017-04-18 08:20:54

标签: sql-server union

我在sql server 2008中编写了一个联合查询。下面是查询:

select cast(i.AMT_SETT as decimal(10,2)) as AMT_SETT 
from  DIB_ERECON_INSIGHT_VISA i

union 
select NULL as AMT_SETT from DIB_ERECON_TRAN_DETAILS d 
where d.channeltype = 'VISA' and cast(fieldno_1 as date) = '18-Apr-2017' and 
not exists(
              select 1 from DIB_ERECON_INSIGHT_VISA i 
              where i.STAN= d.fieldno_38 and replace(d.fieldno_39,':','') = i.TIME_LOC_TRAN and 
              cast(i.Amount as numeric(18,0)) = cast(d.fieldno_31 as numeric(18,0))
              )

我收到此错误:

Error converting data type varchar to numeric.

如果我在''列的第二个查询中尝试null而不是AMTT_SETT,我会收到同样的错误

2 个答案:

答案 0 :(得分:1)

您需要确保AMT_SETT,Amount和fieldno_31列中的所有值都是数值。您可以使用函数ISNUMERIC(yourcolumn)来检测存储在列中的所有非数值。

答案 1 :(得分:1)

第一个select语句出错,有些值是字符串而不是数字,请查看数据并检查哪一行有非数值。

您可以轻松地获得以下行:

SELECT * FROM TABLE WHERE ISNUMERIC(ColumnName)=0