用SQL替换/删除多组字符中的所有文本

问题描述 投票:-2回答:2

结果字段之一在其中包含多个html引用,例如下面的代码。当前结果集如下:

Supervisor  Employee
----------- --------
Supervisor1 <td><br>Employee1<td><br>Employee2<td><br>Employee3</table>
Supervisor2 <td><br>Employee1<td><br>Employee2</table>
Supervisor3 <td><br>Employee1<td><br>Employee2<td><br>Employee3<td><br>Employee4</table>

所需的输出:

Supervisor  Employee
----------- --------
Supervisor1 Employee1 || Employee2 || Employee3
Supervisor2 Employee1 || Employee2
Supervisor3 Employee1 || Employee2 || Employee3 || Employee4

示例查询:

create table #Temp
(
    Supervisor varchar(200), 
    Employee Varchar(200), 
)

INSERT INTO #Temp
VALUES ('Supervisor1', '<td><br>Employee1<td><br>Employee2<td><br>Employee3</table>')
, ('Supervisor2', '<td><br>Employee1<td><br>Employee2</table>')
, ('Supervisor3', '<td><br>Employee1<td><br>Employee2<td><br>Employee3<td><br>Employee4</table>')
select * from #temp
drop table #temp
sql tsql ssms
2个回答
2
投票

您似乎想删除HTML标签。您可以明确地这样做:

select t.*,
       replace(replace(employee, '<td><br>', ' '), '</table>', '') as employees
from t;

0
投票

我在想:

SELECT 
  Supervisor = t.Supervisor,
  Employee   = REPLACE(REPLACE(REPLACE(t.Employee,'<td>',''),'</table>',''),'<br>',' || ')
FROM #Temp AS t;

返回:

Supervisor      Employee
--------------- -----------------------------------------------------------
Supervisor1      || Employee1 || Employee2 || Employee3
Supervisor2      || Employee1 || Employee2
Supervisor3      || Employee1 || Employee2 || Employee3 || Employee4

您可以像这样更新您的数据。

UPDATE #Temp 
SET Employee = REPLACE(REPLACE(REPLACE(Employee,'<td>',''),'</table>',''),'<br>','||');
© www.soinside.com 2019 - 2024. All rights reserved.