简体   繁体   English

未能将 NVARCHAR 转换为 INT,但我不想

[英]Failed to convert NVARCHAR to INT, but I'm not trying to

I get this error:我收到此错误:

Msg 245, Level 16, State 1, Line 5 Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the nvarchar value 'L NOVAK ENTERPRISES, INC' to data type int.将 nvarchar 值“L NOVAK ENTERPRISES, INC”转换为数据类型 int 时转换失败。

I've been wrestling with this query for quite a while and just can't figure out in what place that the conversion is being attempted.我一直在与这个查询搏斗很长一段时间,只是无法弄清楚正在尝试转换的位置。 Using SQL Server 2017.使用 SQL Server 2017。

DECLARE @StartDate AS DateTime
DECLARE @MfgGroupCode AS Varchar(20)

SET @StartDate='2/27/2020'
SET @MfgGroupCode = 'VOLVO_NLA'

SELECT DISTCINT
    CT.No_ AS [Contact Number],
    CT.Name AS [Contact Name],
    UAL.Time AS [Search Date],
    UAL.Param1 AS [Search Part],
    CT.[E-Mail] AS [Contact Email],
    CT.[Phone No_] AS [Contact Phone],
    CT.[Company Name] AS [Search By Customer],
    (SELECT C.Name 
     FROM dbo.[Customer] C 
     WHERE C.No_ = SL.[Sell-to Customer No_] 
       AND C.Name <> '') AS [Sold To Customer],
    SL.[Posting Date] AS [Invoice Date],
    SL.[Document No_] AS [Invoice],
    SL.Quantity AS [Quantity],
    SL.[Unit Price] AS [Unit Price],
    SL.Amount AS [Amount],
    DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) AS [Interval]
FROM
    dbo.[User Action Log] UAL
JOIN
    dbo.[User Action Types] UAT ON UAL.[User Action ID] = UAT.ID
JOIN 
    dbo.[Item] I ON UAL.Param1 = I.[OEM Part Number]
JOIN 
    dbo.[Contact] CT ON UAL.[Contact No_] = CT.No_
LEFT OUTER JOIN 
    dbo.[Sales Invoice Line] SL ON UAL.Param1 = SL.[OEM Part Number] 
                                AND SL.[Posting Date] >= @StartDate
WHERE
    UAT.Name IN ('SinglePartSearch', 'MultiPartSearch')
    AND UAL.[MFG Group Code] = @MfgGroupCode
    AND UAL.Time >= @StartDate
    AND UAL.Param3 > 0
    -- AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) < 0             -- Uncomment to see Current Searches with Past Orders
    -- AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) > -1            -- Uncomment to see Searches resulting in Future Order
    AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) IS NULL   -- Uncomment to See Searches with no Order
ORDER BY
    Interval DESC

Thanks to all of your help and questioning, I was able to identify that the culprit is UAL.Param3 .感谢您的所有帮助和提问,我能够确定罪魁祸首是UAL.Param3 The User Action Log table stores a variety of different "Actions" and the parameters that are affiliated with each type of action (param1, param2, param3).用户操作日志表存储了各种不同的“操作”以及与每种类型的操作相关联的参数(param1、param2、param3)。 For one action "RequestForAccess", the "L NOVAK" value is perfectly acceptable.对于一个动作“RequestForAccess”,“L NOVAK”值是完全可以接受的。 For this query, we're looking at the "SinglePartSearch" and "MultiPartSearch" actions, which will only contain numeric values in UAL.Param3对于此查询,我们正在查看“SinglePartSearch”和“MultiPartSearch”操作,它们仅包含UAL.Param3数值

I replaced this line ( AND UAL.Param3 > 0 ) with ( AND ISNUMERIC(UAL.Param3) = 1 AND UAL.Param3 <> 0 ) in the Where clause and it is now returning the results I hoped for.我在 Where 子句中用( AND ISNUMERIC(UAL.Param3) = 1 AND UAL.Param3 <> 0 )替换了这一行( AND UAL.Param3 > 0 ),现在它返回了我希望的结果。

Let me know if there is a more correct way of doing this and thank you to all who contributed!如果有更正确的方法,请告诉我,并感谢所有贡献者!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM