Search code examples
sqlsql-servert-sqlstring

IndexOf function in T-SQL


Given an email address column, I need to find the position of the @ sign for substringing.

What is the indexof function, for strings in T-SQL?

Looking for something that returns the position of a substring within a string.

in C#

var s = "abcde";
s.IndexOf('c'); // yields 2

Solution

  • CHARINDEX is what you are looking for

    select CHARINDEX('@', '[email protected]')
    -----------
    8
    
    (1 row(s) affected)
    

    -or-

    select CHARINDEX('c', 'abcde')
    -----------
    3
    
    (1 row(s) affected)