Thursday, 26 November 2009

Extract Substring Using SQL

I've been doing some reading on Microsoft forums and I came across quite a lot of posts with questions about string manipulation using various Transact-Sql string manip. functions. It prompted me to write this little snippy-snappy-snoo

One of the questions was about extracting a portion of a string. Here's an example of how to extract a sub string from a string that has consistent seperation of parts. The following Sql will extract and print the third part (number 6717) from the string assigned to the @data variable.

declare @data varchar(100);
set @data = 'Asl/UKE/6717/08';
declare @firstpass varchar(100);
set @firstpass = left(@data, len(@data) - charindex('/', reverse(@data)));
print right( @firstpass, charindex('/', reverse(@firstpass))-1);

The above was tested on Sql Server 2005

0 comments:

Post a Comment

My Posts Go Here

Asp.Net Tips

Avoiding Redirects

C#

CSS Tips

Design Patterns

Registry Pattern

Google Ads

SQL & Database Tips

.Net Config