用一些单词替换可变长度的字符串

编程入门 行业动态 更新时间:2024-10-12 05:54:46
本文介绍了用一些单词替换可变长度的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定下面的字符串

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' )

更多推荐

用一些单词替换可变长度的字符串

本文发布于:2023-11-30 07:04:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   单词   长度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!