SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

INSTR Function

By Nagabhushanam Ponnapalli,

INSTR searches string for substring. --> position is an integer indicating the character of string where SQL Server begins the search. -->If position is negative, SQL Server counts and searches backward from the end of string. occurrence is an integer indicating which occurrence of string SQL Server should search for. The value of occurrence must be positive. The function returns an integer indicating the position of the character in string that is the first character of this occurrence. The default values of both position and occurrence are 1, meaning SQL Server begins searching at the first character of string for the first occurrence of substring. The return value is relative to the beginning of string, regardless of the value of position, and is expressed in characters. If the search is unsuccessful (if substring does not appear occurrence times after the position character of string) the return value is 0.

Total article views: 1166 | Views in the last 30 days: 2
Related Articles

Find the position of all occurrences of an expression within a string

This iTVF returns both the relative and the actual positions of ALL occurrences of a string within a...


SQL BLOG: First Occurrence Of Character In a String

Scenario:  We need to find the first occurrence of character ie non numeric data in the string ...


Get given character's position from a string

Get given character's position from a string


String Search

Search for a string value within columns of data types CHAR, NCHAR, NTEXT, NVARCHAR, TEXT, VARCHAR, ...


Extracting string after and before a Character/Pattern

Finding characters/string before and after some character or pattern