SQL CHARINDEX

Transact-SQL (TSQL) Quick Tips – SQL CHARINDEX Function

Here is a simple example of how the SQL CHARINDEX function works.  You can copy this code into your Query Window and EXECUTE as-is.  The code includes a #temp table with data to illustrate the functionality.  You can modify to suit your needs.

The SQL CHARINDEX function searches an expression for another expression and returns its starting position if found.

SYNTAX:  CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )

Example 1

Query Results:

SQL CHARINDEX

Example 2

Below is a more complex example, using the SQL CHARINDEX Function in tandem with the SQL LEFT Function.

This allows you to dynamically locate the starting position of a character within the player field, and strip out leading characters to create new field player_name

Query Results:

SQL CHARINDEX

 

Additional Content

Check out more examples by visiting my Home Page

Here you will find topics covering  Qlikview  SQL Server  Excel VBA

 

Leave a Comment