Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

sql server - SQL Function to Search in String Values

I would like to create function on sql server to check in string values .

pass to two parameters first original '12.33.44.65.22' , second '44.22' and return

true if parameter2 is found in original parameter ex :

call fun1('22.34.56.78' , '23.24')  -- return false 
call fun1('22.34.56.78' , '23.24') -- return false 
call fun1('22.34.56.78.43.76' , '22.12') -- return false , because should be both exists 
call fun1('22.34.56.78' , '22.56') -- return true 
call fun1('21.34.56.54' , '21.56') -- return true 
call fun1('21.34.56.54' , '34.56') -- return true 
call fun1('22.34.56.78' , '34.55.35') -- return false

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on the updated question, this isn't the answer. You're looking to confirm that all values in string of delimited values exists in another string of delimited values, in this case the delimiter is '.'

select 
  case when charindex('44.22','12.33.44.65.22')>0 then 'true' 
    else 'false' 
    end

Update: if the search string is always in that format, this will work:

create function dbo.func1 (@StringToFind nvarchar(128), @StringToSearch nvarchar(128))
returns nvarchar(5) as
begin
  return case 
          when charindex(left(@StringToFind,charindex('.',@StringToFind)-1),@StringToSearch)>0 
           and charindex(right(@StringToFind,charindex('.',reverse(@StringToFind))-1),@StringToSearch)>0 
          then 'true' 
          else 'false' 
          end;
end
go

select dbo.func1('44.22','12.33.44.65.22')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...