本文介绍了搜索文本是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试搜索输入的字符串是否存在但是它无法正常工作 这是我的代码: DECLARE @ ExpressionToSearch VARCHAR ( 50 ) DECLARE @ ExpressionToFind VARCHAR ( 50 ) SET @ ExpressionToSearch = ' Hello Mr. Joe' SET @ ExpressionToFind = ' Hello Joe' IF CHARINDEX( @ ExpressionToFind , @ ExpressionToSearch )> 0 PRINT ' 是它包含' + ' ' + ' ' + @ ExpressionToFind + ' ' ELSE PRINT ' 它不包含' + ' ' + ' '' + @ ExpressionToFind + ' ' 因为你可以看到我试图找到的表达式但是它说它不包含那个表达式。我不明白为什么?有人能帮助我怎么办?即使有人打字也要工作 Hello Joe没有Mr.或任何没有中间表达式(或字符串)的文本。 谢谢解决方案 不,它没有不存在。 你好乔先生 不包含子表达式 Hello Joe 因为您搜索的字符串中间有Mr.,所以匹配sill不起作用。 您可以使用 DECLARE @ExpressionToSearch VARCHAR ( 50 ) DECLARE @ ExpressionToFind VARCHAR ( 50 ) SET @ ExpressionToSearch = ' 你好乔先生 SET @ ExpressionToFind = ' Hello%Joe' IF @ ExpressionToSearch LIKE @ ExpressionToFind PRINT ' 是它包含' + ' ' + ' ' + @ ExpressionToFind + ' ' ELSE PRINT ' 它没有'' t包含' + ' ' + ' ' + @ ExpressionToFind + ' ' 因为'%'是SQL匹配中的通配符,就像Windows文件名中的'*'一样。 DECLARE @ ExpressionToSearch VARCHAR ( 50 ) DECLARE @ ExpressionToFind VARCHAR ( 50 ) SET @ ExpressionToSearch = ' Hello先生你好' SET @ ExpressionToFind = ' Hello Joe //返回 0 SET @ ExpressionToFind = ' Hello' IF CHARINDEX( @ ExpressionToFind , @ExpressionToSearch )> 0 PRINT ' 是它包含' + ' ' + ' ' + @ ExpressionToFind + ' ' else PRINT ' 它不包含' + ' ' + ' ' + @ ExpressionToFind + ' ' 你不能像'Hello Joe'那样传递 如果你通过,它的回报只有'0' SET @ExpressionToFind = ' Hello' SET @ExpressionToFind = ' Hello Mr。' SET @ExpressionToFind = ' 先生。 Joe' 试试这个: DECLARE @ ExpressionToSearch VARCHAR ( 50 ) DECLARE @ ExpressionToFind VARCHAR ( 50 ) SET @ExpressionToSearch = ' Hello Mr. Joe' SET @ ExpressionToFind = ' Hello Joe' IF patindex('' Hello%joe', @ ExpressionToSearch ) > 0 PRINT ' 是它包含' + ' ' + ' ' + @ ExpressionToFind + ' ' ELSE PRINT ' 它不包含' + ' ' + ' '' + @ ExpressionToFind + ' ' Hi,I am trying to search if a string entered exists or not but it is not working properlythis is my code:DECLARE @ExpressionToSearch VARCHAR(50)DECLARE @ExpressionToFind VARCHAR(50)SET @ExpressionToSearch = 'Hello Mr. Joe'SET @ExpressionToFind = 'Hello Joe'IF CHARINDEX(@ExpressionToFind, @ExpressionToSearch) > 0 PRINT 'Yes it Contains' +' '+'"'+ @ExpressionToFind+'"'ELSE PRINT 'It doesn''t Contain'+' '+ '"'+ @ExpressionToFind+'"'as u can see the expression I am trying to find exist but it is saying it doesn't contain that expression.I do not understand why?Can someone help me how can I make it work even if someone type "Hello Joe" without "Mr." or any text without a middle expression(or string).Thanks 解决方案 No, it doesn't exist.Hello Mr. JoeDoes not contain the subexpressionHello Joebecause the string you are searching has "Mr. " in the middle, so teh match sill not work.You could useDECLARE @ExpressionToSearch VARCHAR(50)DECLARE @ExpressionToFind VARCHAR(50)SET @ExpressionToSearch = 'Hello Mr. Joe'SET @ExpressionToFind = 'Hello%Joe'IF @ExpressionToSearch LIKE @ExpressionToFind PRINT 'Yes it Contains' +' '+'"'+ @ExpressionToFind+'"'ELSE PRINT 'It doesn''t Contain'+' '+ '"'+ @ExpressionToFind+'"'Because '%' is a wild card in SQL matches, much as '*' is in Windows filenames.DECLARE @ExpressionToSearch VARCHAR(50)DECLARE @ExpressionToFind VARCHAR(50)SET @ExpressionToSearch = 'Hello Mr. Joe'SET @ExpressionToFind = 'Hello Joe' //return 0SET @ExpressionToFind = 'Hello'IF CHARINDEX(@ExpressionToFind, @ExpressionToSearch)>0 PRINT 'Yes it Contains' +' '+'"'+ @ExpressionToFind+'"'else PRINT 'It doesn''t Contain'+' '+ '"'+ @ExpressionToFind+'"'you cant pass like 'Hello Joe'if you pass ,its return only '0'SET @ExpressionToFind = 'Hello'SET @ExpressionToFind = 'Hello Mr.'SET @ExpressionToFind = 'Mr. Joe'Try this:DECLARE @ExpressionToSearch VARCHAR(50)DECLARE @ExpressionToFind VARCHAR(50)SET @ExpressionToSearch = 'Hello Mr. Joe'SET @ExpressionToFind = 'Hello Joe'IF patindex('Hello % joe', @ExpressionToSearch) > 0 PRINT 'Yes it Contains' +' '+'"'+ @ExpressionToFind+'"'ELSE PRINT 'It doesn''t Contain'+' '+ '"'+ @ExpressionToFind+'"' 这篇关于搜索文本是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 04:46