用于根据表中的值替换inputtring中的单词的函数

时间:2014-11-06 08:17:56

标签: sql sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012

Word Replacement Table ScreenShot

您好,

我有一个表" tbl_address_replacement",附件是其中数据的屏幕截图。

我的输入字符串是@input =' Auriga Building 4'

我想通过匹配" word_contains"中的值来替换上面的输入字符串。上表中的列,并用" replace_word"替换匹配的值列值。

例如:

所需的o / p:Auriga Bldg。 4

THANKYOU!

6 个答案:

答案 0 :(得分:1)

这对您有所帮助。

示例:SELECT dbo。[fnSplitString1]('Auriga Building 4')

CREATE FUNCTION [dbo].[fnSplitString1] 
( 
     @InputString NVARCHAR(MAX)
 )
 RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @delimiter CHAR(1) 
    DECLARE @start INT, @end INT 
    DECLARE @Result NVARCHAR(MAX)
    DECLARE @Temp AS NVARCHAR(MAX)

    SET @delimiter = ' '
    SET @Result = ''
    ---SET @InputString = 'Auriga Building 4'

    SELECT @start = 1, @end = CHARINDEX(@delimiter, @InputString) 
    WHILE @start < LEN(@InputString) + 1 
    BEGIN 
        IF @end = 0  
            SET @end = LEN(@InputString) + 1      

            SET @Temp = (SELECT TOP 1 replaceword
                        FROM tbl_address_replacement 
                        WHERE word_contains = SUBSTRING(@InputString, @start, @end - @start))


            IF (@Temp IS NULL)
                SET @Result = @Result + SUBSTRING(@InputString, @start, @end - @start) + ' '
            ELSE
                SET @Result = @Result + @Temp + ' '

        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @InputString, @start)        
    END 

    RETURN @Result
END

答案 1 :(得分:1)

试试这个..

 DECLARE @string AS VARCHAR(MAX)
DECLARE @sCurrentString AS VARCHAR(MAX)
DECLARE @sResultString AS VARCHAR(MAX)
DECLARE @Split AS CHAR(1)
DECLARE @X AS xml
DECLARE @sTemp AS VARCHAR(MAX)

SET @sResultString = ''
SET @string = 'Auriga Building 4'
SET @Split = ' '
SET @X = CONVERT(xml,'<root><s>' + REPLACE(@string,@Split,'</s><s>') + '</s></root>')

DECLARE LoopCursor CURSOR FOR
        SELECT T.c.value('.','varchar(max)') FROM @X.nodes('/root/s') T(c)

OPEN LoopCursor

    FETCH NEXT FROM LoopCursor INTO @sCurrentString

    WHILE @@FETCH_STATUS = 0
    BEGIN  
        SET @sTemp = NULL

        SELECT @sTemp = replace_word FROM tbl_address_replacement WHERE word_contains = @sCurrentString

         IF (ISNULL(@sTemp, '') = '')
             SET @sTemp = @sCurrentString

        SET @sResultString = @sResultString + @Split + @sTemp 

        FETCH NEXT FROM LoopCursor INTO @sCurrentString
    END

SELECT @sResultString

答案 2 :(得分:1)

我在做系统迁移的同时写了这样的东西。

以下是适用的代码:

DECLARE @rowcount int;

-- Repeat until there are no iterations left (lazy recursive method)
SET @rowcount = 937;
WHILE (@rowcount > 0)
  BEGIN
    UPDATE addresses
    SET    address_line_1 = Replace(x.address_line_1, ' ' + y.word_contains + ' ', ' ' + y.replace_word + ' ')
    FROM   addresses As x
     INNER
      JOIN tbl_address_replacement As y
        ON x.address_line_1 LIKE '% ' + y.word_contains + ' %'
    ;

    SET @rowcount = @@RowCount;
  END
;

我发现这是最有效的方法,因为它减少了所需的迭代次数。

基本上它会一直循环,直到update语句不再进行更改!

答案 3 :(得分:0)

你需要一个游标来循环tbl_address_replacement

中的每一行

这是函数

create function CorrectifyInput(@input nvarchar(max)) returns nvarchar(max)
as
begin
declare @wordContains nvarchar(100), @replaceWord nvarchar(100)
declare cur cursor for select * from tbl_address_replacement
open cur
fetch next from cur into @wordContains, @replaceWord
while @@FETCH_STATUS = 0 begin
    set @input = replace(@input, @wordContains, @replaceWord)
    fetch next from cur into @wordContains, @replaceWord
end
return @input
end

答案 4 :(得分:0)

它绝对不是最好的方式,但是从我的头脑中我想出了这个。

SET NOCOUNT ON
DECLARE @Input NVARCHAR(50)
DECLARE @AdressReplacement TABLE([Contains] NVARCHAR(50), [Replace] NVARCHAR(50))

INSERT INTO @AdressReplacement VALUES
    ('Building','Bldg.'),
    ('Building.','Bldg.')

SET @Input = 'Auriga Building. 4'

IF NOT EXISTS(SELECT [Replace] FROM @AdressReplacement WHERE @Input LIKE '%'+[Replace]+'%')
SET @Input = 
    REPLACE --After replacing old value with new value replace all '..' with '.'
    (
        REPLACE 
        (
            @Input,--What to replace
            (SELECT TOP 1[Contains] FROM @AdressReplacement WHERE @Input LIKE '%'+[Contains]+'%'),--Old value
            (SELECT TOP 1[Replace] FROM @AdressReplacement WHERE @Input LIKE '%'+[Contains]+'%')--New value
        ),
    '..',
    '.'
    )
PRINT @Input

答案 5 :(得分:0)

如果您在地址中只有一个要替换的字符串,则可以使用我的示例:

UPDATE A 
SET A.address = REPLACE(A.address, AR2.word_contains, AR2.replace_word)
FROM #tbl_address AS A
CROSS APPLY (
    SELECT A.address, word_contains, AR.replace_word 
    FROM #tbl_address_replacement AS AR
    WHERE PATINDEX('%'+AR.word_contains+'%', A.address) > 0
        ) AS AR2;

对于变量替换,您可以使用:

SELECT @input = Replace(@input, word_contains, replace_word) FROM #tbl_address_replacement  

如果有人会寻找其他选择(如果有人真的不喜欢循环)。替换我们可以与CTE一起使用:

;with Cte(a)  AS (
    SELECT address As [a] 
    FROM #tbl_address
    UNION ALL
    SELECT CAST(REPLACE(A.a, AR2.word_contains, AR2.replace_word) AS VARCHAR(200)) AS [a]
    FROM Cte AS A
    CROSS APPLY (
        SELECT A.a, word_contains, AR.replace_word 
        FROM #tbl_address_replacement AS AR
        WHERE PATINDEX('%'+AR.word_contains+'%', A.[a]) != 0
            ) AS AR2

)
SELECT A.a 
FROM Cte AS A
WHERE NOT EXISTS (
    SELECT 1 
    FROM #tbl_address_replacement AS AR
    WHERE PATINDEX('%'+AR.word_contains+'%', A.a) != 0
        ) ;