且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果输入字符串在表行中有子字符串,如何替换输入字符串中的字符串?

更新时间:2023-11-06 22:45:04

try

<pre>

declare @table table ( col nvarchar(50) )

insert into @table values ('ACT ONLY')
insert into @table values ('NSW ONLY')
insert into @table values ('QLD ONLY')

declare @find  nvarchar(50)
declare @result  nvarchar(50)
set @find ='ABC - QLD ONLY'
set  @result =    ( select top 1 col from @table where @find like '%'+col+'%')
if(@result is not null )
begin
 select @result = REPLACE(@find , @result ,'') 
  select @result = REPLACE(@result , '-' ,'') 
end
select  @result -- ABC


这应该也工作

This should work too
DECLARE @temp TABLE (Col1 VARCHAR(50))
DECLARE @input VARCHAR(50)

SET @input = 'ABC - QLD ONLY'

INSERT INTO @temp
	SELECT 'ACT ONLY' UNION SELECT 'NSW ONLY' UNION SELECT 'QLD ONLY'

SELECT REPLACE(REPLACE(@input, col1, ''),' - ','') FROM @temp
WHERE CHARINDEX(col1,@input)  > 0