1

I am not sure whether T-SQL supports regular expressions like other SQL engines. So far I found that I can only use PATINDEX but its matching support seems to be limited.

Here is what I want to for some field in a table using T-SQL:

  • Find a substring between " IP Address: " and ","
  • Replace it by " IP Address: ****,"

Here is a more concrete example:

SELECT some_magic("The access is from IP ADDRESS: www.xx.yyy.zz, ...")

I want this to return "The access is from IP ADDRESS: ****, ...".

Is it possible to do this in T-SQL? I am using Azure SQL Database.

2
  • As it is it's not clear for me, can you please provide a clear sample data and the desired results? Commented Sep 12, 2019 at 20:33
  • @Sami I tried to add a concise example. Please let me know if it is not clear. Commented Sep 12, 2019 at 20:46

3 Answers 3

2

You can use STUFF() and REPLICATE() functions as

WITH C AS
(
  SELECT Str,
         CHARINDEX(':', Str) +1 S,
         CHARINDEX(',', Str) -1 E
  FROM
  (
    VALUES
    ('The access is from IP ADDRESS: www.xx.yyy.zz, ...')
  ) T(Str)
)
SELECT Str,
       STUFF(Str, S, (E - S)+1, REPLICATE('X', E - S)) Result
       --You could change 'X' with '*' and if you want just 4 stars then get ride of REPLICATE() and use '****'
FROM C;

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Good solution, and if he want to only 4 characters, he can just use '***' instead of replicate.
1

@sami beat me to it. Here's what I was working on before he posted:

DECLARE @string VARCHAR(1000) = 'The access is from IP ADDRESS: www.xx.yyy.zz, ...';

SELECT 
  [start] = fnStart.Here,
  [stop]  = fnStop.Here,
  ItemLen = fnStop.Here-fnStart.Here,
  SUBSTRING(@string,fnStart.Here,fnStop.Here-fnStart.Here),
  STUFF(@string,fnStart.Here+1,fnStop.Here-fnStart.Here-1,'****')
FROM        (VALUES('IP ADDRESS: '))                                      AS 
search(string)
CROSS APPLY (VALUES(CHARINDEX(search.string,@string)+LEN(search.string))) AS fnStart(Here)
CROSS APPLY (VALUES(CHARINDEX(',',@string,fnStart.Here+1)))               AS fnStop(Here);

Here's another way using PatternSplitCM. It extracts the IP address, it's length as well as returning the value you are looking for:

DECLARE @string VARCHAR(1000) = 'The access is from IP ADDRESS: www.xx.yyy.zz, ...';

SELECT
  ItemLen   = fnStop.Here-fnStart.Here,
  IPAddress = SUBSTRING(@string,fnStart.Here,fnStop.Here-fnStart.Here),
  NewString = STUFF(@string,fnStart.Here+1,fnStop.Here-fnStart.Here-1,'****')
FROM        (VALUES('IP ADDRESS: '))                                      AS search(string)
CROSS APPLY (VALUES(CHARINDEX(search.string,@string)+LEN(search.string))) AS fnStart(Here)
CROSS APPLY (VALUES(CHARINDEX(',',@string,fnStart.Here+1)))               AS fnStop(Here);

Results:

ItemLen     IPAddress         NewString                                   
----------- ----------------- ---------------------------------------------
14           www.xx.yyy.zz    The access is from IP ADDRESS: ****, ...

Comments

0

Here, the other two methods are:

DECLARE @START_WORD VARCHAR(100)='IP Address:'
DECLARE @END_CHAR CHAR(1)=','

DECLARE @SAMPLE AS TABLE (TEST_DATA VARCHAR(250))

INSERT INTO @SAMPLE (TEST_DATA)
VALUES ('The access is from IP ADDRESS: www.xx.yyy.zz, ...')

SELECT TEST_DATA,
REPLACE(TEST_DATA,
        (
            SUBSTRING((SUBSTRING (TEST_DATA,0,CHARINDEX(@END_CHAR,TEST_DATA))),
            CHARINDEX(@START_WORD,(SUBSTRING (TEST_DATA,0,CHARINDEX(@END_CHAR,TEST_DATA))))+LEN(@START_WORD),
            LEN((SUBSTRING (TEST_DATA,0,CHARINDEX(@END_CHAR,TEST_DATA)))))
        ),
        '****') AS EXPECTED_TEST_DATA_METHOD1,
REPLACE(TEST_DATA,
        (
            SUBSTRING ((RIGHT(TEST_DATA, LEN(TEST_DATA) - PATINDEX('%'+@START_WORD+'%', TEST_DATA)-LEN(@START_WORD) )),
            0,
            CHARINDEX(@END_CHAR,(RIGHT(TEST_DATA, LEN(TEST_DATA) - PATINDEX('%'+@START_WORD+'%', TEST_DATA)-LEN(@START_WORD) ))))
        ),
        '****') AS EXPECTED_TEST_DATA_METHOD2
FROM @SAMPLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.