SQL select varchar substring from middle of varchar

I recently had to pull out a substring from a larger string in SQL.  The problem was that I required 7 digits from an 20 digit varchar value.

So, the id field contained:


and I wanted the 7 digits from the middle of the varchar as highlighted in bold below:



The following SQL statement achieved this for me:

SELECT substring(id from 11 for 7) as new_id from persons.

This left me with a value of S3390W5.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: