SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Extracting a String From Between Two Delimiting Characters

By Stephen Lasham, 2004/06/14

Total article views: 9650 | Views in the last 30 days: 105

Extracting a string from between two delimiting characters

The problem

A table contains a column (particulars) consisting of a semi-colon-delimited string of text, for which the text between the first and second semi-colon needs to be extracted.  The extracted text will be no longer than 99 characters.

The position of the first and second semi-colons is variable and the second semi-colon may not be present.

Extract the text between the semi-colons.

Sample data

Table : Sample_table

Column : Particulars

LDR ; LUC20031026901 ; Vehicle

LDR ; Consolidated

LDR ; SUB35030172701 ; Building

LDRR ; LIQ200310121 ; Liquor

Solution

SELECT Particulars,
   CASE
	-- when no second semi-colon,
	WHEN CHARINDEX(';',(SUBSTRING(Particulars,CHARINDEX(';',Particulars,1)+1,99))) = 0
	-- then extract the full string from first semi-colon to the max length of 99
		THEN LTRIM(RTRIM(SUBSTRING(Particulars,CHARINDEX(';',Particulars,1)+1,99)))
	-- else extract the full string from the first semi-colon up to second semi-colon
	ELSE LTRIM(RTRIM(SUSTRING(
       /* <text>> */	particulars, 
       /* <start > */	CHARINDEX(';',Particulars,1) + 1,
       /* <length> */	CHARINDEX(';', SUBSTRING(Particulars, 
		       			CHARINDEX(';', Particulars, 1) + 1, 
				     	99)) - 1 
        ) ) )
	END    AS Result_string
 FROM Sample_table

Resulting data

Particulars

Result_string

LDR ; LUC20031026901 ; Vehicle

LUC20031026901

LDR ; Consolidated

Consolidated

LDR ; SUB35030172701 ; Building

SUB35030172701

LDR ; LIQ200310121 ; Liquor

LIQ200310121

Method in Detail

SUBSTRING is used to extract the actual string value required.

	SUBSTRING(<text>, <start>, <length>)

	<text> = Particulars

CHARINDEX is used to identify the position of the semi-colons within the string.
CHARINDEX(, , )

The start position is easy, being the position of the first semi-colon plus 1 character.

	 = CHARINDEX(';', Particulars, 1) + 1

The string length is the length of the text from <Start_pos> to the second semi-colon. This requires a second SUBSTRING to get a revised string of all text following the first semi-colon. A CHARINDEX over this revised text (less one character) gives the length.

<length> = CHARINDEX(';', <revised_text>, 1) - 1

<revised_text> = SUBSTRING(Particulars, <start>, <len>)

				<start> = CHARINDEX(';',Particulars,1) + 1

				<len> = 99 (maximum)

Substituting these back into the original SUBSTRING function gives

SUBSTRING(
/* <text> */	particulars, 
/* <start > */	CHARINDEX(';', Particulars, 1) + 1,
/* <length> */	CHARINDEX(';', SUBSTRING(Particulars, 
					CHARINDEX(';', Particulars, 1) + 1, 
					99)) - 1 
)

This now only needs to be wrapped in LTRIM and RTRIM functions to remove leading and trailing blanks.

Additional code was also added to handle the condition where no second semi-colon was present.

Alternative Method

You can of course come up with other methods of doing this for example, the ELSE statement above can be substituted with:

-- else extract the full string from the first semi-colon up to second semi-colon
ELSE RTRIM(LTRIM(LEFT(
	-- This is the text following the first semi-colon up to the max length of 99
	SUBSTRING(Particulars,CHARINDEX(';',Particulars,1)+1,99), 
	-- This is the length, one character before the next semi-colon
	CHARINDEX(';',SUBSTRING(Particulars,CHARINDEX(';',Particulars,1)+1,99)) - 1
) ) ) 

Summary

You have seen demonstrated a method of extracting text from between two embedded delimiting characters. This has made use of the SUBSTRING, CHARINDEX, LTRIM and RTRIM functions.

I have searched for simpler ways to do this without resorting to a cursor, but as yet have not found one. Any alternative suggestions would be most welcome.

By Stephen Lasham, 2004/06/14

Total article views: 9650 | Views in the last 30 days: 105
Your response
 
 
Related tags

Advanced Querying    
T-SQL    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com