在nvarchar字段上使用LEAST与int一起使用的int不能正常工作?

时间:2014-05-01 20:21:16

标签: sql sql-server tsql

我正在尝试创建一个我联合4个表的视图。其中一个字段是文档编号。我可以从其中一个表中提取相关文档编号的唯一方法是在最左边的6个字符上运行LEFT。但是当我这样做然后CAST到整数我仍然无法查询该字段。这就像它正在查看我的where子句并试图转换该字段中的所有字符。

这就是我正在做的事情:

SELECT     dbo.OSLP.U_RepStatus AS RepStatus, dbo.OSLP.SlpCode, dbo.OPCH.CardCode, dbo.OPCH.CardName, 'IN' AS DocTypeDesc, dbo.OPCH.DocNum, 
                      dbo.OPCH.DocEntry, dbo.OPCH.DocDate, dbo.OPCH.NumAtCard, dbo.OPCH.DocStatus, dbo.OPCH.DocTotal, dbo.OPCH.U_CommInvNo AS ARInvNo
FROM         dbo.OPCH INNER JOIN
                      dbo.OCRD ON dbo.OPCH.CardCode = dbo.OCRD.CardCode INNER JOIN
                      dbo.OSLP ON dbo.OCRD.U_SalesRepNumber = dbo.OSLP.SlpCode
WHERE     (dbo.OSLP.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.OPCH.U_CommInvNo IS NOT NULL)
UNION
SELECT     OSLP_1.U_RepStatus AS RepStatus, OSLP_1.SlpCode, dbo.ORPC.CardCode, dbo.ORPC.CardName, 'CM' AS DocTypeDesc, dbo.ORPC.DocNum, dbo.ORPC.DocEntry, 
                      dbo.ORPC.DocDate, dbo.ORPC.NumAtCard, dbo.ORPC.DocStatus, - 1 * dbo.ORPC.DocTotal AS DocTotal, dbo.ORPC.U_CommInvNo AS ARInvNo
FROM         dbo.ORPC INNER JOIN
                      dbo.OCRD AS OCRD_1 ON dbo.ORPC.CardCode = OCRD_1.CardCode INNER JOIN
                      dbo.OSLP AS OSLP_1 ON OCRD_1.U_SalesRepNumber = OSLP_1.SlpCode
WHERE     (OSLP_1.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.ORPC.U_CommInvNo IS NOT NULL)
UNION
SELECT     T3.U_RepStatus AS RepStatus, T3.SlpCode, T0.ContraAct AS CardCode, T2.CardName, 'JE' AS DocTypeDesc, T1.TransId AS DocNum, T1.TransId AS DocEntry, 
                      T1.RefDate AS DocDate, T0.LineMemo AS NumAtCard, T1.BtfStatus AS DocStatus, T0.Debit AS DocTotal, CAST(LEFT(T0.LineMemo, 6) AS INT) AS ARInvNo
FROM         dbo.JDT1 AS T0 INNER JOIN
                      dbo.OJDT AS T1 ON T0.TransId = T1.TransId INNER JOIN
                      dbo.OCRD AS T2 ON T0.ContraAct = T2.CardCode INNER JOIN
                      dbo.OSLP AS T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE     (T0.ShortName = 'CompanyName') AND (T1.TransType = 30) AND (T0.Account = '_SYS00000000624') AND (T3.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T0.Debit > 0)
UNION
SELECT     T7.U_RepStatus AS RepStatus, T7.SlpCode, T4.ContraAct AS CardCode, T6.CardName, 'JE' AS DocTypeDesc, T5.TransId AS DocNum, T5.TransId AS DocEntry, 
                      T5.RefDate AS DocDate, T4.LineMemo AS NumAtCard, T5.BtfStatus AS DocStatus, - (1 * T4.Credit) AS DocTotal, CAST(LEFT(T4.LineMemo, 6) AS INT) AS ARInvNo
FROM         dbo.JDT1 AS T4 INNER JOIN
                      dbo.OJDT AS T5 ON T4.TransId = T5.TransId INNER JOIN
                      dbo.OCRD AS T6 ON T4.ContraAct = T6.CardCode INNER JOIN
                      dbo.OSLP AS T7 ON T6.U_SalesRepNumber = T7.SlpCode
WHERE     (T4.ShortName = 'CompanyName') AND (T5.TransType = 30) AND (T4.Account = '_SYS00000000624') AND (T7.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T4.Credit > 0)

如果我使用WHERE ARInvNo = 295696查询此视图,我收到此错误: Msg 245,Level 16,State 1,Line 1 将nvarchar值'A / P In'转换为数据类型int时,转换失败。

如果我从我的视图中取出底部2个联合,我可以在没有问题的情况下查询WHERE ARInvNo = 295696。如果我运行查询的后两部分,我的结果中没有任何内容包含'A / P In',尽管在我正在使用的WHERE标准之外的JDT1中存在行。知道为什么SQL会查看我的WHERE语句并在返回任何数据之前尝试LEFT和CAST每个字段吗?

澄清一下,这不会返回任何结果:

SELECT T0.LineMemo 
FROM JDT1 T0  INNER JOIN OJDT T1 ON T0.TransId = T1.TransId INNER JOIN OCRD T2 ON T0.ContraAct = T2.CardCode INNER JOIN OSLP T3 ON T2.U_SalesRepNumber = T3.SlpCode 
WHERE T0.ShortName = 'CompanyName' AND T1.TransType = 30 AND T0.Account = '_SYS00000000624' AND T3.U_RepStatus IN ('Dir', 'Ind', 'Sal') AND left(T0.LineMemo,3)='A/P In'

1 个答案:

答案 0 :(得分:0)

我现在看到了。

它正在' A&P'在你的价值观中。它无法生成' /' INT的字符

MSSQL cast( [varcharColumn] to int) in SELECT gets executed before WHERE clause filters out bad values

简而言之,你会想要这样做:

select (case when isnumeric(TO.LineMemo) = 1 then cast(TO.LineMemo as int) end)