0

Dumb question, but can't seem to find a quick answer... I am creating UDF to replace name prefix with a nested replace... something like this:

REPLACE(
 REPLACE(
  REPLACE(
   REPLACE(
    REPLACE(
     REPLACE(
      REPLACE(
       REPLACE(
        @Name
       , 'Mr ', '')
      , 'Mrs ', '')
     , 'Ms ', '')
    , 'Dr ', '')
   , 'Mr. ', '')
  , 'Mrs. ', '')
 , 'Ms. ', '')
, 'Dr. ', '')

but want to replace those containing a '.' BEFORE those without. What's the order/hierarchy of nested replace? TIA, Danny

3
  • Perhaps you could make use of the esoteric parsename function like... PARSENAME(@name, 1) which will lop off everything before the . character. Example here. This will be a LOT less expensive than a million nested replaces or a UDF for that matter. Commented Oct 3, 2018 at 14:24
  • Thanks for suggestion... I know mine is not most efficient method, but was pressed for time. :-) Commented Oct 3, 2018 at 14:31
  • For later readers... you can introduce new problems by hacking off everything before the period when working with names as some people use a first initial instead of a full first name. If you know you will have a prefix in front of every name, then you're fine. Data sets I work with with will never let me be so lucky. Think F. Murry Abraham, or F. Scott Fitzgerald, or S. Epatha Merkerson. Commented Jan 16, 2024 at 14:51

2 Answers 2

2

Your inner most Replace will always run first on the character string. So from inner to outer is the order in which they will be executed. I suggest though trying a more elegant approach perhaps through the use of regex and case statements so it might be easier to maintain rather than just having a ton of nested replaces which don't necessarily all do work.

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

Comments

0

I suggest using a function like this in SQL:

CREATE FUNCTION [dbo].[ReplaceNamePrefix]
(@value NVARCHAR(50)
)
RETURNS NVARCHAR(50)
AS
     BEGIN
         DECLARE @RETURN NVARCHAR(50);
         SET @RETURN = @value;
         SET @RETURN = REPLACE(@RETURN, 'Mrs.', ''); 
         SET @RETURN = REPLACE(@RETURN, 'Mrs', '');
         SET @RETURN = REPLACE(@RETURN, 'Mr.', '');
         SET @RETURN = REPLACE(@RETURN, 'Mr', '');
         SET @RETURN = REPLACE(@RETURN, 'Ms.', '');
         SET @RETURN = REPLACE(@RETURN, 'Ms', ''); 
         SET @RETURN = REPLACE(@RETURN, 'Dr. ', '');
         SET @RETURN = REPLACE(@RETURN, 'Dr', ''); ');
         SET @RETURN = RTRIM(LTRIM(@RETURN));
         RETURN @RETURN;
     END;

Use the following command to get your goal:

SELECT [dbo].[ReplaceNamePrefix]('Dr. Danny Dennison');

the result is: Danny Dennison

enter image description here

But if you insist on using your own code, use the following:

SELECT REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( 'MR  Danny Dennison', 'Mrs.', '' ), 'Mrs', '' ), 'Mr.', '' ), 'Mr', '' ), 'Ms.', '' ), 'Ms', '' ), 'Dr. ', '' ), 'Dr', '' );

2 Comments

If you replace Mr, you will not find Mr., Mrs or Mrs. in the string later on...
I did create a function. Again, this was something I needed to do quickly with 'easy' code I'm familiar with, but may revise using @BehrouzMoslem's "SET" methodology... assume performance would improve as well. Thanks all!

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.