男孩,这是一口......
我想从字符串中解析标记。令牌可以是单词或短语,我想要的是用字符串替换任何标记的每个出现。我想在不使用光标的情况下这样做。
实施例
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
我想要的是一个内联函数,它将用''(空字符串)替换字符串中找到的任何标记列表。
答案 0 :(得分: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
答案 1 :(得分: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