I am facing an issue with the REPLACE function in SQL Server when dealing with HTML data stored in my database. It works fine when I use a hardcoded string, but doesn’t seem to function correctly when I retrieve the content from a column in a table.
Example where it works using a hardcoded string:
DECLARE @htmlContent varchar(MAX) = '<div>
<section>
<article>
<div>
<div class="btn-wrapper" style="width: 200px; height: 40px; margin: auto;">
<button type="button" style="background: __ThemeColor__; border-radius: 5px; font-family: Arial, sans-serif; color: __TextColor__; padding: 12px 30px;">
<span style="color: __TextColor__; font-weight: bold;">
<a style="color: __TextColor__; text-decoration: none;" href="__ActionURL__">Accept</a>
</span>
</button>
</div>
</div>
</article>
</section>
</div>'
SELECT @htmlContent = REPLACE(@htmlContent,
'<button type="button" style="background: __ThemeColor__; border-radius: 5px; font-family: Arial, sans-serif; color: __TextColor__; padding: 12px 30px;">
<span style="color: __TextColor__; font-weight: bold;">
<a style="color: __TextColor__; text-decoration: none;" href="__ActionURL__">Accept</a>
</span>
</button>',
'<button type="button" style="background: __ThemeColor__; border-radius: 5px; font-family: Arial, sans-serif; color: __TextColor__; padding: 12px 30px;">
<span style="color: __TextColor__; font-weight: bold;">
<a style="color: __TextColor__; text-decoration: none;" href="__ActionURL__">Accept</a>
</span>
</button>
<div style="display: __ShowOption__;">
<p style="font-family: Arial, sans-serif; font-size: 12px; color: #888; text-align: center; margin-top: 10px;">
<a style="color: #666; text-decoration: underline;" href="__DeclineURL__">decline.option.text</a>
</p>
</div>')
PRINT @htmlContent
Example that does not work using data from the database:
DECLARE @htmlContent VARCHAR(MAX) = (SELECT TOP 1 HtmlContent
FROM [EmailTemplates]
WHERE HtmlContent LIKE '%class="btn-wrapper"%')
SELECT @htmlContent = REPLACE(@htmlContent,
'<button type="button" style="background: __ThemeColor__; border-radius: 5px; font-family: Arial, sans-serif; color: __TextColor__; padding: 12px 30px;">
<span style="color: __TextColor__; font-weight: bold;">
<a style="color: __TextColor__; text-decoration: none;" href="__ActionURL__">Accept</a>
</span>
</button>',
'<button type="button" style="background: __ThemeColor__; border-radius: 5px; font-family: Arial, sans-serif; color: __TextColor__; padding: 12px 30px;">
<span style="color: __TextColor__; font-weight: bold;">
<a style="color: __TextColor__; text-decoration: none;" href="__ActionURL__">Accept</a>
</span>
</button>
<div style="display: __ShowOption__;">
<p style="font-family: Arial, sans-serif; font-size: 12px; color: #888; text-align: center; margin-top: 10px;">
<a style="color: #666; text-decoration: underline;" href="__DeclineURL__">decline.option.text</a>
</p>
</div>')
PRINT @htmlContent
The REPLACE function performs as expected with the hardcoded content, but it does not yield results when applied to the data fetched from the database. Does anyone know the reason for this discrepancy?