错误-无效的长度参数传递给LEFT或SUBSTRING函数

时间:2018-11-20 08:54:12

标签: sql sql-server tsql

我不是开发人员,但我是在这里寻求优秀开发人员的帮助。我做的是非常基本的SQL,但是复杂的SQL却没有下面的提示。

SELECT     aualdocs.fmt_acc, aualdocs.ddn_cd1, dbo.aualrefn.dsc_no1, aunrmast.nam_fam, RTRIM(aunrmast.nam_gv1) + ' ' + RTRIM(COALESCE (aunrmast.nam_gv2, '')) 
                  AS nam_gvn, aunrmast.dte_brt, aunrmast.sex, aunrmast.nam_ttl, 
                  CASE WHEN res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %' THEN res_ad1 END AS pst_nme, 
                  CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN LEFT(res_ad1,PATINDEX('% %',res_ad1)) END as pst_num,
                  CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN SUBSTRING(res_ad1,CHARINDEX(' ',res_ad1),charindex(' ',res_ad1,charindex(' ',res_ad1)+1) - CHARINDEX(' ',res_ad1)) END AS pst_str,
                  CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN REVERSE(LEFT(REVERSE(RTRIM(res_ad1)),PATINDEX('% %',REVERSE(RTRIM(res_ad1)))-1)) END AS pst_typ,
                  REPLACE(res_ad2,aunrmast.pst_cde,'') AS pst_loc,
                  aunrmast.pst_cde, auprstad.bld_nme, auprstad.pcl_unt, auprstad.unt_alp, auprstad.hou_num, auprstad.hou_alp, auprstad.hou_end, auprstad.end_alp, ausrmast.str_nme, 
                  ausrmast.str_typ, RTRIM(ausrsubr.sbr_nme) + '  ' + ausrsubr.sta_nme AS sub_sta, ausrsubr.pst_cde AS Expr1, aualdocs.ddf_cd2, aualdocs.dda_cd2, auallnks.lnk_typ, 
                  aunrlink.nar_num, auprlink.pcl_num, aualdocs.det_dte, aualdocs.dte_no2, aualdocs.ddn_cd2
FROM         dbo.aualdocs AS aualdocs INNER JOIN
                  dbo.auallnks AS auallnks ON aualdocs.fmt_acc = auallnks.src_acc INNER JOIN
                  dbo.aunrlink AS aunrlink ON auallnks.src_acc = aunrlink.mdu_fmt INNER JOIN
                  dbo.aunrmast AS aunrmast ON aunrlink.nar_num = aunrmast.nar_num INNER JOIN
                  dbo.auprlink AS auprlink ON aualdocs.fmt_acc = auprlink.mdu_fmt INNER JOIN
                  dbo.auprstad AS auprstad ON auprlink.pcl_num = auprstad.pcl_num INNER JOIN
                  dbo.ausrmast AS ausrmast ON auprstad.str_num = ausrmast.str_num AND auprstad.str_blk = ausrmast.str_blk INNER JOIN
                  dbo.ausrsubr AS ausrsubr ON ausrmast.sbr_num = ausrsubr.sbr_num INNER JOIN
                  dbo.aualrefn ON aualdocs.ddf_cd1 = dbo.aualrefn.ref_val
WHERE     (aualdocs.doc_typ = 90) AND (auallnks.lnk_typ = 151) AND (aualdocs.det_cde = 'APPR') AND (dbo.aualrefn.ref_typ = 'wrd_num')

此查询最初运行良好,但最近被触发时发出错误:

  

传递给LEFT或SUBSTRING函数的长度参数无效。

我不知道从哪里开始寻找。希望有人可以提供帮助。

如果有任何想法,您可能需要将解决方案复制并粘贴到注释中,以便我在数据库上尝试一下,以便查看是否可行。我正在运行的是使用SQL Studio的MSSQL。

1 个答案:

答案 0 :(得分:1)

更新查询中的LEFTSUBSTRING函数。尝试如下。似乎LEFT中的第二个参数值或SUBSTRING中的第三个参数值是负值。

SELECT     aualdocs.fmt_acc, aualdocs.ddn_cd1, dbo.aualrefn.dsc_no1, aunrmast.nam_fam, RTRIM(aunrmast.nam_gv1) + ' ' + RTRIM(COALESCE (aunrmast.nam_gv2, '')) 
        AS nam_gvn, aunrmast.dte_brt, aunrmast.sex, aunrmast.nam_ttl, 
        CASE WHEN res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %' THEN res_ad1 END AS pst_nme, 
        CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN LEFT(res_ad1, IIF(PATINDEX('% %',res_ad1) > 0, PATINDEX('% %',res_ad1), 0)) END as pst_num,
        CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN SUBSTRING(res_ad1, CHARINDEX(' ',res_ad1), IIF(charindex(' ',res_ad1, charindex(' ',res_ad1)+1) - CHARINDEX(' ',res_ad1) > 0, charindex(' ',res_ad1, charindex('',res_ad1)+1) - CHARINDEX(' ',res_ad1), 0))  END AS pst_str,
        CASE WHEN NOT(res_ad1 LIKE 'C/%' OR res_ad1 LIKE 'PO %') THEN REVERSE(LEFT(REVERSE(RTRIM(res_ad1)), IIF((PATINDEX('% %',REVERSE(RTRIM(res_ad1)))-1) > 0, (PATINDEX('% %',REVERSE(RTRIM(res_ad1)))-1), 0))) END AS pst_typ,
        REPLACE(res_ad2,aunrmast.pst_cde,'') AS pst_loc,
        aunrmast.pst_cde, auprstad.bld_nme, auprstad.pcl_unt, auprstad.unt_alp, auprstad.hou_num, auprstad.hou_alp, auprstad.hou_end, auprstad.end_alp, ausrmast.str_nme, 
        ausrmast.str_typ, RTRIM(ausrsubr.sbr_nme) + '  ' + ausrsubr.sta_nme AS sub_sta, ausrsubr.pst_cde AS Expr1, aualdocs.ddf_cd2, aualdocs.dda_cd2, auallnks.lnk_typ, 
        aunrlink.nar_num, auprlink.pcl_num, aualdocs.det_dte, aualdocs.dte_no2, aualdocs.ddn_cd2
FROM         dbo.aualdocs AS aualdocs INNER JOIN
        dbo.auallnks AS auallnks ON aualdocs.fmt_acc = auallnks.src_acc INNER JOIN
        dbo.aunrlink AS aunrlink ON auallnks.src_acc = aunrlink.mdu_fmt INNER JOIN
        dbo.aunrmast AS aunrmast ON aunrlink.nar_num = aunrmast.nar_num INNER JOIN
        dbo.auprlink AS auprlink ON aualdocs.fmt_acc = auprlink.mdu_fmt INNER JOIN
        dbo.auprstad AS auprstad ON auprlink.pcl_num = auprstad.pcl_num INNER JOIN
        dbo.ausrmast AS ausrmast ON auprstad.str_num = ausrmast.str_num AND auprstad.str_blk = ausrmast.str_blk INNER JOIN
        dbo.ausrsubr AS ausrsubr ON ausrmast.sbr_num = ausrsubr.sbr_num INNER JOIN
        dbo.aualrefn ON aualdocs.ddf_cd1 = dbo.aualrefn.ref_val
WHERE     (aualdocs.doc_typ = 90) AND (auallnks.lnk_typ = 151) AND (aualdocs.det_cde = 'APPR') AND (dbo.aualrefn.ref_typ = 'wrd_num')