SQL内部加入需要帮助

时间:2016-04-05 19:44:57

标签: sql integer inner-join varchar

我在下面有以下查询:

SELECT 
[dbo].[history].histtable,
[dbo].[history].code,
[dbo].[history].Date,
dbo.Activities.Number,
dbo.table1.Itemnumber

FROM [dbo].[History]
INNER JOIN 
[dbo].[Activities]
ON
[dbo].[History].Flags=[dbo].[Activities].Objects

INNER JOIN dbo.table1 
On
CONVERT (Int, dbo.table1.Itemnumber) = CONVERT (INT, dbo.history.Date)
OR CONVERT (Int, dbo.tablenumber.Itemnumber) = dbo.Activities.Alltransactions

ORDER BY 3 DESC

我一直收到以下错误:

  

将varchar值Itemnumber转换为数据类型int时转换失败。

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以为要加入的每个表单独选择,并使用ISNUMERIC仅提取非数字的行,以便更好地识别问题并查看哪些值不是数字

类似

select Itemnumber
from dbo.table1 
where IsNumeric(Itemnumber) = 0

或在你加入,像

INNER JOIN dbo.table1 
On IsNumeric(dbo.table1.Itemnumber) = 1 
and 
(
    CONVERT (Int, dbo.table1.Itemnumber) = CONVERT (INT, dbo.history.Date)
OR
    CONVERT (Int, dbo.tablenumber.Itemnumber) = dbo.Activities.Alltransactions
)