Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...