本文介绍了子串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我在名为action_taken的表中有一个列名称sender_index_code.现在我正尝试使用sender_index_code搜索表.

sender_index_code的某些值例如

5/2012-D-prog
7/2011-U-sec
16/2012-R-secids



现在,我让用户仅输入数字部分并搜索表,就像当用户输入5/2012时,将向他显示sender_index_code 5/2012-D-prog的记录.

为此,我正在使用此查询

select * from action_taken where  substring(sender_index_code,1,charindex(''-'',sender_index_code,0)-1) =''5/2012''



但它告诉我

Invalid length parameter passed to the LEFT or SUBSTRING function.


的错误
当我在SQL服务器中将这个查询分为两个历史记录进行测试时,它们如何工作正常.

例如,我首先找到-"的字符索引.

select CHARINDEX (''-'',''5/2012-D-prog'',0)-1


在这里我使用-1是因为我想在``-''
之前获取值
然后使用此char索引提取第一个''-''
之前的字符串

select SUBSTRING(''5/2012-D-prog'',1,6)



abd它给了我正确的答案,即5/2012,但是整个查询作为一个整体都无法正常工作. -1 = -1,但索引本身从0开始,并且没有元素位于-1索引.

例如,如果5/2012D是值之一,则在子字符串函数中,它变为
substring(sender_index_code,1,(0-1))会抛出错误
尝试

 选择 * 来自 action_taken 其中 '  5/2012' =
  CASe  何时 charindex(' -',sender_index_code, 0 )> 0 然后 substring(sender_index_code, 1 ,charindex(' -',sender_index_code, 0 )-1)
其他 ' '
结束 



Hello!!

I have a column name sender_index_code in a table called action_taken. now i ma trying to search into the table using sender_index_code.

Some values of sender_index_code are like

5/2012-D-prog
7/2011-U-sec
16/2012-R-secids



now i am letting user enter only the number part and search the table like when user enter 5/2012 he will be shown the records for the sender_index_code 5/2012-D-prog.

for this i am using this query

select * from action_taken where  substring(sender_index_code,1,charindex(''-'',sender_index_code,0)-1) =''5/2012''



but it shows me error that

Invalid length parameter passed to the LEFT or SUBSTRING function.



how ever when i am breking this query into two pasts for test purpose in my sql server they are working fine.

for example i first find the char index of ''-''.

select CHARINDEX (''-'',''5/2012-D-prog'',0)-1


here i have used -1 because i want to get values fore the ''-''

then use this char index to extract string before the first ''-''

select SUBSTRING(''5/2012-D-prog'',1,6)



abd it gives me the right answer i.e 5/2012 but the whole query as a whole is not working

解决方案



这篇关于子串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:00