T-SQL子串替换(2017年翻译)

时间:2018-02-23 17:22:42

标签: sql sql-server tsql replace translate

在我的工作中,我们的SQL Server版本是2012年,我相信,在2017版本中,现在有一个TRANSLATE命令。因为我们只有2012 SQL Server,所以我需要一些UPDATE和REPLACE的组合(REPLACE(REPLACE显然可以执行与一个TRANSLATE语句相同的操作。

我的表有一个SKU列,WebDescription列和一个HTML_WebDescription列。我的目标是在WebDescription中使用文本,并在必要时将其替换为

标记,然后将其放在HTML_Column中。

到目前为止......我有:

UPDATE TableauWarehouse_Test.dbo.EntityAttributes_WebCoreInfo_WebDescription 
SET HTML_WebDescription = '<p><strong>' + 
REPLACE(REPLACE(REPLACE(WebCoreInfo_WebDescription, CHAR(13) + CHAR(10) + 
CHAR(13) + CHAR(10),'</p>'+ CHAR(13) + CHAR(13) +'<p><strong>'), 
CASE WHEN DIFFERENCE(CHAR(13),'.') <= 6 THEN '.' ELSE 'jfjdkfjsk' END, 
'.</strong>'), 
CASE WHEN DIFFERENCE('<strong>','<strong>') > 4 THEN '.</strong>' ELSE 
'HFJKDJSK' END, '???')+ '</p>'

它返回:

<p><strong>Serious Performance Bumper.</strong> Prepare your Jeep for outdoor adventures with the Fab Fours JK Lifestyle winch bumper .</strong> This bumper is designed to accept a winch with up to 10,000 lbs.</strong> of force in pulling power.</strong> It's a capable unit that offers a secure winch mount.</strong> The bumper does not feature a guard and has an attractive black style to help it blend in with your Jeep well.</strong> </p>

<p><strong>Serious Strength.</strong> This Fab Fours JK Lifestyle winch bumper is composed of 3/16" steel and powder coated with a two stage black matte coating.</strong> It offers additinoal tire clearance and is built to last.</strong> </p>

<p><strong>Includes Lights.</strong> This Fab Fours JK Lifestyle winch bumper comes with both fog and turn indicators built in, as well as injection molded light housings for adding additional lights if you want a brighter front end.</strong> </p>

<p><strong>Easy Bolt Install.</strong> This Fab Fours JK Lifestyle winch bumper goes on in just two hours with basic steps from beginning to end.</strong> Just lock the bumper itself down as well as all the accessories with bolts.</strong> The mounting hardware is included with the kit and putting the bumper in place takes just two hours to complete.</strong> </p>

<p><strong>Lifetime Protection.</strong> Fab Fours offers a reliable lifetime limited warranty on this Jeep bumper.</strong> It's protected to prevent manufacturer defects and other similar issues.</strong> </p>

<p><strong>Application.</strong> This Fab Fours JK Lifestyle Winch Bumper without Grille Guard is specifically designed as a replacement front bumper for 2007-2018 Jeep Wrangler JK, 2 and 4-door models.</strong></p>

我只需要正确关闭我的强力标签(即应用程序。)。现在它正在每个时期之后放置一个收盘强标签。

谢谢大家!

1 个答案:

答案 0 :(得分:0)

T-SQL中的DIFFERENCE函数不是您所期望的,它与SOUNDEX函数有关。

要查找字符串中字符的位置,请使用CHARINDEX函数。

在这里,我在每行第一次出现一个点后添加了</strong>

SELECT '<p><strong>' 
    + LEFT(WebCoreInfo_WebDescription,ISNULL(NULLIF(CHARINDEX('.',WebCoreInfo_WebDescription),0),LEN(WebCoreInfo_WebDescription)))
    +'</strong>' + ISNULL(SUBSTRING(WebCoreInfo_WebDescription,NULLIF(CHARINDEX('.',WebCoreInfo_WebDescription),0)+1,LEN(WebCoreInfo_WebDescription)),'')
    + '</p>'
FROM TableauWarehouse_Test.dbo.EntityAttributes_WebCoreInfo_WebDescription

我使用NULLIF函数来处理CHARINDEX返回0时的情况(当该行中没有点时)。