1

UDF are not my cup of tea sadly and I'm using SQL2014

I want to use a UDF to replace text in a string by looking up the value to be replaced from a table with the replacement value in the same row in another column I managed to find this example below but that only removes the last row for the lookup table

Here is the one that is not quite right so I'm looking for one that will work please

USE [WebProductDataBase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[Ufn_ReplaceSeller]
(
 @Value   varchar(max)
)
RETURNS  varchar(max)
AS
BEGIN

declare @Return   varchar(max)

SELECT @return = Replace(@Value, LookupValue, ReplaceValue) FROM dbo.Dim_Seller

RETURN @return
End

Here is an extract from my lookup table (currently the value we need to replaceValue is set to blank as that's is what we need to remove the LookupValue from the string) See example of the string below with the result needed

SellerID    Name            LookupValue      ReplaceValue   SellerUrl   DataID
AW2485      ActivInstinct   ActivInstinct                               1
AW6342      All Outdoor     All Outdoor                                 2
AW3023      Ambr            Amb                                         3
AW3469      Amp Bos         Amp Bos                                     4
AW988       Beau Expert     Beau Expert                                 5
AW222       Sloggi          Sloggi                                      6 
AW333       Ladies          Ladies                                      7

Source Row Field    Result Needed
Ladies 1 Pack Sloggi Invisible Supreme Midi Briefs
Ladies 1 Pack Sloggi Invisible Supreme Midi Briefs
Ladies 1 Pack Sloggi EverNew Cotton Midi Briefs
Ladies 1 Pack Sloggi EverNew Cotton Midi Briefs

The Better Option would be a multi replace function as I will need one to lookup and remove multiple values going forward
Source Row Field Result Needed Ladies 1 Pack Sloggi Invisible Supreme Midi Briefs Invisible Supreme Briefs Ladies 2 Packs Sloggi EverNew Cotton Midi Briefs EverNew Cotton Briefs

So the LookupValue would be     ReplaceValue
Sloggi                          Blank
Ladies                          Blank
Midi                            Blank
Packs                           Blank
Pack                            The rest below would also be blank for now
1   
2   
3   
4   
5   
6   
7   
8   
9   
0   

By The A. This has spaces in front and at the end

This list will be quite long by the end of the project

1 Answer 1

2

I think the following will work:

BEGIN
    declare @Return varchar(max);

    SET @Return = @Value;

    SELECT @return = Replace(@return, LookupValue, ReplaceValue)
    FROM dbo.Dim_Seller;

    RETURN @return;
End;

I'm not a big fan of using variables in functions like this (modifying the variable and setting it at the same time). This version should work, however, and is much simpler to code than a while loop using the table or a cursor and should be faster than a recursive CTE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.