问题描述
我有代码来规范化POB地址。例如,包括的规范化之一是: set @ string = replace(@ string,'pobox','pob' )
现在我想做类似的事情 - 我想找到任何直接跟随的POB数字(两者之间没有空格)并插入一个空格。我想找到像POB [0-9]的模式,然后用POB替换POB。我该如何做到这一点?可以用简单的替换来完成吗?或者我需要使用一些其他功能,如PATINDEX?
是的,你是正确的,你可以使用
Declare @searchstring varchar(50)
Set @searchstring ='POB9090'
If(PatIndex('POB [0-9]%',@ searchString)> 0)
开始
设置@searchstring =替换(@searchString,'POB','POB')
结束
选择@searchString
或者可能更好的方法是使用 case
语句,以便它可以很容易地纳入 select
语句
声明@searchstring varchar(50)
设置@searchstring ='POB9090'
选择案例
当PatIndex('POB [0-9]%',@searchString)> 0然后替换(@searchString,'POB','POB')
Else @searchString
结束'SearchString'
I have code to normalize a POB address. For example, one of the normalizations included is:
set @string=replace(@string,'pobox','pob')
Now I want to do something similar - I want to find any POB that is directly followed by a number (without a space in between) and insert a space. I want to find the pattern "like POB[0-9]" and then replace the "POB" with "POB ". How can I accomplish this? Can it be done with a simple replace? Or do I need to use some other function, like PATINDEX?
Yes you are correct you can use PATINDEX
Declare @searchstring varchar(50)
Set @searchstring = 'POB9090'
If (PatIndex('POB[0-9]%', @searchString) > 0)
Begin
Set @searchstring = Replace(@searchString, 'POB', 'POB ')
End
Select @searchString
Or probably a nicer way would be to use a case
statement so that it can be easily incorporated in to a select
statement
Declare @searchstring varchar(50)
Set @searchstring = 'POB9090'
Select Case
When PatIndex('POB[0-9]%', @searchString) > 0 Then Replace(@searchString, 'POB', 'POB ')
Else @searchString
End 'SearchString'
这篇关于如何使用T-SQL替换模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!