charindex

Description

Returns an integer representing the starting position of an expression.

Syntax

charindex(expression1, expression2 [, start])

Parameters

expression

is a binary or character column name, variable, or constant expression. Can be char, varchar, nchar, nvarchar, unichar, univarchar, binary, text_locator, unitext_locator, image_locator or varbinary.

start

when specified, causes the search for expression1 to start at the given offset in expression2. When start is not given, the search start at the beginning of expression2. start can be an expression, but must return an integer value.

Examples

Example 1

Returns the position at which the character expression “wonderful” begins in the notes column of the titles table:

select charindex("wonderful", notes) 
from titles 
where title_id = "TC3218"
-----------
        46

Example 2

This query executes successfully, returning zero rows. The column spt_values.name is defined as varchar(35):

select name
from spt_values
where charindex( 'NO', name, 1000 ) > 0

In comparison, this query does not use start, returning the position at which the character expression “wonderful” begins in the notes column of the titles table:

select charindex("wonderful", notes) 
from titles 
where title_id = "TC3218"

-----------
        46

Usage

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

Any user can execute charindex.

See also

Documentation Transact-SQL Users Guide

Function patindex