将MySQL转换为SQL Server后传递给正确函数的长度参数无效

时间:2017-06-08 13:01:51

标签: sql sql-server

我收到错误

  

传递给RIGHT函数的长度参数无效

当我运行这个sql脚本

SELECT count(*) as count 
FROM damage_reports AS a 
JOIN comments AS b ON b.v_id = a.v_id AND b.comment_txt LIKE ('Damage Report #%') 
JOIN VIN AS c ON a.v_id = c.v_id 
JOIN damage_report_entries AS d ON a.dr_id = d.dr_id 

WHERE LEFT(RIGHT(b.comment_txt, LEN(b.comment_txt) - CHARINDEX('#',b.comment_txt)), 
CHARINDEX(':',RIGHT(b.comment_txt, LEN(b.comment_txt) - CHARINDEX('#',b.comment_txt) -1)))=
convert(nvarchar(10), d.dr_id)
AND a.dateIncident BETWEEN '2017-04-26' AND '2017-06-05' 

造成问题的正确功能是第二个权利。如果我删除-1它运行,但给出错误的结果。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您很可能有一些不符合RIGHT条款的数据。当您在结尾处省略-1时,您将获得结果,尽管某些字符串将为空或NULL - 当您说您在没有-1的情况下获得不正确的数据时,这就是您所指的。

为了捕获这些值,请尝试对您的数据库运行以下查询。无论你在这里得到什么结果都是那些负责搞乱你的查询的人。

SELECT 
    count(*) as [count ]
FROM damage_reports AS a 
JOIN comments AS b ON b.v_id = a.v_id AND b.comment_txt LIKE ('Damage Report #%') 
JOIN VIN AS c ON a.v_id = c.v_id 
JOIN damage_report_entries AS d ON a.dr_id = d.dr_id 
WHERE LEN(LEFT(
    RIGHT(b.comment_txt, LEN(b.comment_txt) - CHARINDEX('#',b.comment_txt)), 
    CHARINDEX(':',RIGHT(b.comment_txt, LEN(b.comment_txt) - CHARINDEX('#',b.comment_txt)))))<= 0
AND a.dateIncident BETWEEN '2017-04-26' AND '2017-06-05'   

获得这些结果后,您将获得调整联接所需的必要记录。

祝你好运。