now musing at www.aliaspooryorik.com!

March 19, 2008

Using CharIndex to get a SubString

Filed under: SQL statements — aliaspooryorik @ 10:53 am

If you need to extract part of a string based on the index of a character in SQL, then you can use the CharIndex function and the Substring function. This works the same as the Find & Mid functions in ColdFusion and IndexOf in JavaScript.

For example  the order_deliverycountry field holds values in the format ‘UK|United Kingdom’. To get the ‘UK’ part use the following code:

select SubString([Orders].order_deliverycountry, 1, CharIndex('|', Orders.order_deliverycountry) - 1) as CountryCode
from [Countries]

1 Comment »

  1. But what if I want the United Kingdom part?

    Comment by steve — August 21, 2008 @ 2:48 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.