Replacing variable length string with some word(用一些单词替换可变长度字符串)
问题描述
给定下面的字符串
Declare @string varchar(max)='abc ___________ deffns ___ cg _ hif _______hh ihs';
这是所需的输出:(每个连字符,无论长度如何,都应替换为姓氏)
this is the Output required : (Every hypen irrespective of length should be replaced with lastname)
abc lastname deffns lastname cg lastname hif lastname hh ihs
这里的问题是,可以有很多可变长度的Hypens
(最大长度可以是<20)...
The issue here is, there can be many Hypens
of variable length(max length can be <20)...
我尝试了许多方法,并使用以下方法解决..
I tried with many methods and settled with below approach..
select
REPLACE(REPLACE(replace(stringcol,replicate('_',20),'LASTNAME'),
replicate('_',19),'LASTNAME'),
replicate('_',18),'LASTNAME')
from table
有没有办法有效地做到这一点..任何建议将是最受欢迎的
Is there a way to do accomplish this efficiently..any advice would be most welcome
推荐答案
先去掉多个下划线,再进行替换.
First get rid of the multiple underscores, then do the replace.
这是一种方法:
select replace(replace(replace(@string, '_', '><'
), '<>', ''
), '><', 'LASTNAME'
)
这篇关于用一些单词替换可变长度字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用一些单词替换可变长度字符串


基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01