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
?
SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive. Tip: Also look at the STUFF() function.
We use regular expressions to define specific patterns in T-SQL in a LIKE operator and filter results based on specific conditions. We also call these regular expressions as T-SQL RegEx functions. In this article, we will use the term T-SQL RegEx functions for regular expressions.
Description. The Oracle/PLSQL REGEXP_REPLACE function is an extension of the REPLACE function. This function, introduced in Oracle 10g, will allow you to replace a sequence of characters in a string with another set of characters using regular expression pattern matching.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With