【问题标题】:SQL query to remove script tags from table column data从表列数据中删除脚本标签的 SQL 查询
【发布时间】:2022-01-22 19:29:31
【问题描述】:

我有一张表A 有几列; NameDescription 存储字符串数据,类型为 nvarchar(250)nvarchar(max)

有没有办法清理这些列中的错误数据以删除脚本标签(如果有的话)?这是目前的数据:

注意: 代码正在后端编写,以去除表格中新条目的标签。

【问题讨论】:

  • 查看REPLACE()函数。
  • 我需要对列中的所有值都使用相同的值,我需要执行子字符串和 concat 之类的操作,并使用经过清理的值更新数据。 @RahulKPandey
  • 看看这是否有帮助DECLARE @y NVARCHAR(MAX) = '<script>My textMore text.</script>' SELECT y.value('.', 'NVARCHAR(MAX)') FROM ( SELECT y = CAST(REPLACE(REPLACE(@y, '>', '/>'), '</', '<') AS XML) ) r
  • 是的,这行得通,但它对单个列值很有用,我该如何处理整个列值? @RahulKPandey

标签: sql


【解决方案1】:

试试这个:

输入:

col1    Name    Description
1   abstrat<script> testing<script>
2   abstrat100<script>  testing12<script>
3   abstrat101<script>  testing223<script>
4   abstrat102<script>  testing334<script>
5   abstrat103<script>  testing335<script>
6   abstrat104<script>  testing336<script>
NULL

UPDATE  dbo.StackExample
    set Name = coalesce((   SELECT top 1
                                CASE 
                                    WHEN Name LIKE '%<script>%' THEN SUBSTRING(Name, 0, charindex('<', Name, 0)) 
                                END AS Name1
                            FROM dbo.StackExample
                           ), Name)



UPDATE  dbo.StackExample
    set Description = coalesce((    SELECT top 1
                                CASE 
                                    WHEN Description LIKE '%<script>%' THEN SUBSTRING(Description, 0, charindex('<', Description, 0)) 
                                END AS Description1
                            FROM dbo.StackExample
                           ), Name)



SELECT  *
FROM dbo.StackExample

输出:

col1    Name    Description
1   abstrat testing
2   abstrat testing
3   abstrat testing
4   abstrat testing
5   abstrat testing
6   abstrat testing
NULL    abstrat testing

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2016-05-02
    • 2021-03-09
    • 2011-10-31
    • 2018-06-28
    • 2017-02-27
    相关资源
    最近更新 更多