SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Slice out string from within specified delimiters

By anonuser,

Scalar function 'f_delimited' slices out of a passed-in string, from a specified position inside the string, that segment of it which is delimited on one or both sides by a specified delimiter.

Example:  Print out a segment of a 'syscomments.text' column for a stored procedure 'p_proc', representing a line of code (i.e., the segment of 'syscomments.text' between carriage returns) containing the string '@retc'.

Given:  @line_of_code VARCHAR (200), @pos INT, @delim CHAR (1)

SELECT @delim        = CHAR (13) -- carriage return
     , @pos          = CHARINDEX ('@retc', text)
     , @line_of_code = dbo.f_delimited
                         (@f_string = text
                        , @f_delim  = @delim
                        , @f_pos    = @pos)
FROM syscomments
WHERE id    = OBJECT_ID ('p_proc')
  AND colid = 1

Total article views: 91 | Views in the last 30 days: 1
Related Articles

Get String between two delimiters

Get string between two delimiters


Delimited String Parser

Parses delimited string into a table of up to 9 varchar fields.


Extracting Rows from Delimited Strings

Extracting Rows from Delimited Strings


Split string using multiple delimiters

This script is used to split the string using multiple delimiters


Delimited String Parsing Functions - Big Set

Feed it large strings of delimited horizontal data and it returns it back as a vertical table.