英文:
Invalid length parameter passed to the LEFT or SUBSTRING function error?
问题
以下是已翻译的内容:
左连接
WORKQUEUE_INFO wqi
在 wqi.WORKQUEUE_ID = SUBSTRING(hxcmt.hx_comment,CHARINDEX('[',hxcmt.hx_comment)+1,(CHARINDEX(']',hxcmt.hx_comment)-CHARINDEX('[',hxcmt.hx_comment))-1)
HX_COMMENT 返回类似以下数值:
'Deferred until 09/13/2022 12:00 AM CDT on workqueue ADMIN-CATCH ALL QUEUE [203]'
所以,我尝试的是在 HX_COMMENT 中的括号值上进行连接(从上述值中获取的 203)。
有什么原因可能导致这种情况失败?
英文:
I have seen a few of these questions here but can't find something that applies to my code. Here is my snipet where I am getting the error:
left join
WORKQUEUE_INFO wqi
on wqi.WORKQUEUE_ID = SUBSTRING(hxcmt.hx_comment,CHARINDEX('[',hxcmt.hx_comment)+1,(CHARINDEX(']',hxcmt.hx_comment)-CHARINDEX('[',hxcmt.hx_comment))-1)
HX_COMMENT returns values like this:
'Deferred until 09/13/2022 12:00 AM CDT on workqueue ADMIN-CATCH ALL QUEUE [203]'
So what I am trying to do is join on the value in the brackets in HX_COMMENT (203 from the above value)
Any ideas why this might be failing?
答案1
得分: 1
成为一个小问题来陷阱/消除零值
示例
Declare @YourTable Table ([HX_COMMENT] varchar(150)) Insert Into @YourTable Values
('... CDT on workqueue ADMIN-CATCH ALL QUEUE [203]')
,('... CDT on workqueue ADMIN-CATCH ALL QUEUE [999] other text')
,('... CDT on workqueue ADMIN-CATCH ALL QUEUE -- No Brackets')
Select A.*
,NewVal = substring([HX_COMMENT],P1+1,P2-P1-1)
From @YourTable A
Cross Apply ( values (nullif(charindex('[',[HX_COMMENT]),0),nullif(charindex(']',[HX_COMMENT]),0) ) ) B(P1,P2)
结果
英文:
It becomes a small matter to trap/nullify zero values
Example
Declare @YourTable Table ([HX_COMMENT] varchar(150)) Insert Into @YourTable Values
('... CDT on workqueue ADMIN-CATCH ALL QUEUE [203]')
,('... CDT on workqueue ADMIN-CATCH ALL QUEUE [999] other text')
,('... CDT on workqueue ADMIN-CATCH ALL QUEUE -- No Brackets')
Select A.*
,NewVal = substring([HX_COMMENT],P1+1,P2-P1-1)
From @YourTable A
Cross Apply ( values (nullif(charindex('[',[HX_COMMENT]),0),nullif(charindex(']',[HX_COMMENT]),0) ) ) B(P1,P2)
Results
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论