4

小伙子,真是一口……

我想从字符串中解析出标记。标记可以是单词或短语,我想要的是用字符串替换任何标记的每次出现。我想在不使用光标的情况下做到这一点。

前任。

declare @str varchar(256) = 'I want this type to be left and of type to be gone. the and a should also be gone of course remains'

create table #Tokens (token varchar(50))
go

insert table (token) values ('of type')
insert table (token) values ('a')
insert table (token) values ('the')
insert table (token) values ('to')
insert table (token) values ('of')
go

我想要的是一个内联函数,它将用''(空字符串)替换字符串中找到的任何标记列表。

4

2 回答 2

11

一个非常简单的答案是使用以下内容:

USE tempdb;

DECLARE @str VARCHAR(256);

SET @str = 'I want this type to be left and of type to be gone.
           the and a should also be gone of course remains';

CREATE TABLE #Tokens (token VARCHAR(50));
INSERT INTO #Tokens (token) VALUES ('of type');
INSERT INTO #Tokens (token) VALUES ('a');
INSERT INTO #Tokens (token) VALUES ('the');
INSERT INTO #Tokens (token) VALUES ('to');
INSERT INTO #Tokens (token) VALUES ('of');

SELECT @str = REPLACE(@str, token, '')
FROM #Tokens;

SELECT @str;

DROP TABLE #Tokens;

返回:

I wnt this type  be left nd   be gone.  nd  should lso be gone  course remins
于 2013-09-18T21:48:52.753 回答
3

我将它与 REAL 表一起使用。

CREATE FUNCTION [dbo].[funStripSpecialCharacters]
(
   @inputString as varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
    select @inputString = REPLACE(@inputString, SpecialCharacter, '') 
      from SpecialCharacters
    RETURN @inputString
END
于 2013-09-18T21:45:46.970 回答