0

I have an html string stored as a record:

<div>          
<p>##RecipientFirstName##,</p>          
<p>##CustomerProjectOwnerName## with ##CustomerName## has approved your...</p>         </div>  

I want to replace the items inside the hash tags with actual names stored in the person table. I have tried this but it returns 3 different records. I need one record returned with the items inside the hash tags replaced with the Replace statement. My code:

SELECT mtg.Template,  REPLACE(mtg.Template, '##RecipientFirstName##',(SELECT p.FirstName FROM dbo.Person p WHERE p.PersonId = 16)) , 
                      REPLACE(mtg.Template, '##CustomerProjectOwnerName##',(SELECT p.FirstName FROM dbo.Person p WHERE p.PersonId = 16))   
 FROM dbo.MessageTypeGlobal mtg
WHERE mtg.MessageTypeGlobalId = 1

2 Answers 2

1

You need to nest your replace statements:

SELECT REPLACE(
               REPLACE(mtg.Template, '##RecipientFirstName##', p.FirstName), 
       , '##CustomerProjectOwnerName##', p.FirstName) 
FROM dbo.MessageTypeGlobal mtg
CROSS APPLY
(
    SELECT FirstName FROM dbo.Person WHERE PersonId = 16
) p
WHERE mtg.MessageTypeGlobalId = 1

I've used cross apply to prevent the need to write the subquery over and over again.

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

Comments

1

I think this is what you are trying to do (if you need different subqueries):

select customtemplate = replace(replace(replace(mtg.Template
    , '##CustomerProjectOwnerName##'
      , (select p.FirstName from dbo.Person p where p.PersonId = 16) )
    , '##RecipientFirstName##'
      , (select p.FirstName from dbo.Person p where p.PersonId = 16) )
    , '##CustomerName##'
      , (select p.FirstName from dbo.Person p where p.PersonId = 16) )
 from dbo.MessageTypeGlobal mtg
where mtg.MessageTypeGlobalId = 1

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.