SQL Server,将字符串中的字符替换为表中的值

发布于 2025-01-27 17:15:28 字数 2018 浏览 4 评论 0原文

如何用表中的值替换字符串中的值?

例如,

    select *
into #t
from 
(
    select 'bla'c1,'' c2 union all
    select 'table'c1,'TABLE' c2 union all
    select 'value'c1,'000' c2 union all
    select '...'c1,'' c2
)t1

declare @s nvarchaR(max)='this my string and i want to replace all values that are in table #t'

我的表中有一些值,我想用字符串中的C2替换C1。

结果应该是

这是我的字符串,我想替换表#T

中的所有000

中的所有000 我用CLR解决了问题

using System;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Data.Linq;

namespace ReplaceValues
{
    public partial class Functions
    {
        [SqlFunction
            (
                //DataAccess = DataAccessKind.Read,
                SystemDataAccess = SystemDataAccessKind.Read
            )
        ]
        public static string ReplaceValues(string row, string delimitator, string values, string replace/*, bool CaseSensitive*/)
        {
            //return row;
            string[] tmp_values = values.Split(new string[] { delimitator }, StringSplitOptions.None);
            string[] tmp_replace = replace.Split(new string[] { delimitator }, StringSplitOptions.None);

            row = row.ToUpper();

            for (int i = 0; i < Math.Min(tmp_values.Length, tmp_replace.Length); i++)
            {
                row = row.Replace(tmp_values[i].ToUpper(), tmp_replace[i]);
            }

            return row;
        }

    }
}

,然后

select *
into #t
from 
(
    select 'value1'OldValue,'one'NewValue union all
    select 'value2'OldValue,'two'NewValue union all
    select 'value3'OldValue,'three'NewValue union all
    select 'value4'OldValue,'four'NewValue
)t1


select dbo.ReplaceValues(t1.column,'|',t2.v,t2.r)
from MyTable t1
cross apply
(
    select dbo.inlineaggr(i1.OldValue,'|',1,1)v,
           dbo.inlineaggr(i1.NewValue,'|',1,1)r
    from #t i1
)t2

我必须对其进行改进,以更好地管理案件敏感,但性能还不错。 (也是“ inlineaggr”是我几年前写的CLR)

how can i replace values in string with values that are in a table?

for example

    select *
into #t
from 
(
    select 'bla'c1,'' c2 union all
    select 'table'c1,'TABLE' c2 union all
    select 'value'c1,'000' c2 union all
    select '...'c1,'' c2
)t1

declare @s nvarchaR(max)='this my string and i want to replace all values that are in table #t'

i have some values in my table and i want to replace C1 with C2 in my string.

the results should be

this my string and i want to replace all 000 that are in TABLE #t

UPDATE:
i solved with a CLR

using System;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Data.Linq;

namespace ReplaceValues
{
    public partial class Functions
    {
        [SqlFunction
            (
                //DataAccess = DataAccessKind.Read,
                SystemDataAccess = SystemDataAccessKind.Read
            )
        ]
        public static string ReplaceValues(string row, string delimitator, string values, string replace/*, bool CaseSensitive*/)
        {
            //return row;
            string[] tmp_values = values.Split(new string[] { delimitator }, StringSplitOptions.None);
            string[] tmp_replace = replace.Split(new string[] { delimitator }, StringSplitOptions.None);

            row = row.ToUpper();

            for (int i = 0; i < Math.Min(tmp_values.Length, tmp_replace.Length); i++)
            {
                row = row.Replace(tmp_values[i].ToUpper(), tmp_replace[i]);
            }

            return row;
        }

    }
}

and then

select *
into #t
from 
(
    select 'value1'OldValue,'one'NewValue union all
    select 'value2'OldValue,'two'NewValue union all
    select 'value3'OldValue,'three'NewValue union all
    select 'value4'OldValue,'four'NewValue
)t1


select dbo.ReplaceValues(t1.column,'|',t2.v,t2.r)
from MyTable t1
cross apply
(
    select dbo.inlineaggr(i1.OldValue,'|',1,1)v,
           dbo.inlineaggr(i1.NewValue,'|',1,1)r
    from #t i1
)t2

i have to improved it to manage better the case sensitive, but performance are not bad.
(also 'inlineaggr' is a CLR i wrote years ago)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2025-02-03 17:15:28

您可以通过递归执行此操作。假设您有一个发现对的表,则可以编号行,然后使用递归CTE:

create table #t(c1 nvarchar(100), c2 nvarchar(100));
insert into #t(c1, c2) values
('bla', ''),
('table', 'table'),
('value', '000'),
('...', '');

declare @s nvarchar(max) = 'this my string and i want to replace all values that are in table #t';

with ncte as (
    select row_number() over (order by (select null)) as rn, *
    from #t
), rcte as (
    select rn, replace(@s, c1, c2) as newstr
    from ncte
    where rn = 1

    union all

    select ncte.rn, replace(rcte.newstr, ncte.c1, ncte.c2)
    from ncte
    join rcte on ncte.rn = rcte.rn + 1
)
select *
from rcte
where rn = 4

You can do this via recursion. Assuming you have a table of find-replace pairs, you can number the rows and then use recursive cte:

create table #t(c1 nvarchar(100), c2 nvarchar(100));
insert into #t(c1, c2) values
('bla', ''),
('table', 'table'),
('value', '000'),
('...', '');

declare @s nvarchar(max) = 'this my string and i want to replace all values that are in table #t';

with ncte as (
    select row_number() over (order by (select null)) as rn, *
    from #t
), rcte as (
    select rn, replace(@s, c1, c2) as newstr
    from ncte
    where rn = 1

    union all

    select ncte.rn, replace(rcte.newstr, ncte.c1, ncte.c2)
    from ncte
    join rcte on ncte.rn = rcte.rn + 1
)
select *
from rcte
where rn = 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文