[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Pattern Searching in SQL

Posted on 2016-07-14
5
Medium Priority
?
43 Views
Last Modified: 2016-07-18
I am trying to figure out how to use the replace function by using a pattern.  There may be another function that can do what I am looking for.  In SQL I have the following string as an example:

20151130\Mailbox[1965661]\Mailbox[1965661]\Top of Information Store[1965662]\Suggested Contacts[1984960]

Open in new window


What I would like to do is remove the characters [ ] and anything in between.  So the output would be:

20151130\Mailbox\Mailbox\Top of Information Store\Suggested Contacts

Open in new window


Any direction would be greatly appreciated.
0
Comment
Question by:rye004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 41711447
you an use STUFF statement for this, run this few time, each time it updates one []
Update your table set YourColumn = STUFF(yourcolumn, CHARINDEX('[', YourColumn),9,'')
where CHARINDEX('[', yourColumn ) > 0

I am assuming that the string length between [ and ] is 8 characters
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 41711452
You will need to use a function since you need the replace to occur multiple times per string.  Below is a sample function that you can adjust to fit your needs by simply changing the delimiter characters in the @Start and @End assignments.

CREATE FUNCTION dbo.StripHTMLTags
(
	@Text NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS

BEGIN
	DECLARE @Start INT,
		@End INT
		
	SET @Start = CHARINDEX('<', @Text)
	SET @End = CHARINDEX('>', @Text, @Start)

	WHILE @Start > 0 AND @End > 0
	BEGIN
		SET @Text = STUFF(@Text, @Start, @End - @Start + 1, '')
		SET @Start = CHARINDEX('<', @Text)
		SET @End = CHARINDEX('>', @Text, @Start)
	END
	RETURN LTRIM(RTRIM(@Text))
END

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41711465
Or make @start and @end parameters :)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 41713171
While it won't be the fastest way, doing it recursively is fairly simple.
Before running the code, correct the "UNION A_L_L" -- my filter at work doesn't allow U A.

IF OBJECT_ID('tempdb.dbo.#data') IS NOT NULL
    DROP TABLE #data
CREATE TABLE #data (
    id int IDENTITY(1, 1) PRIMARY KEY,
    string varchar(8000)
    )
INSERT INTO #data VALUES('20151130\Mailbox[1965661]\Mailbox[1965661]\Top of Information Store[1965662]\Suggested Contacts[1984960]')

;WITH cte_replace AS (
    SELECT id, STUFF(string, CHARINDEX('[', string), CHARINDEX(']', string) - CHARINDEX('[', string) + 1, '') AS string, 1 AS level
    FROM #data
    WHERE string LIKE '%\[%\]%' ESCAPE '\'
    UNION A_L_L
    SELECT id, STUFF(string, CHARINDEX('[', string), CHARINDEX(']', string) - CHARINDEX('[', string) + 1, '') AS string, level + 1 AS level
    FROM cte_replace
    WHERE string LIKE '%\[%\]%' ESCAPE '\'
)
SELECT id, string
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY level DESC) AS row_num
    FROM cte_replace
) AS derived
WHERE row_num = 1
0
 

Author Closing Comment

by:rye004
ID: 41717595
This worked great.  Thank you.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question