Posts

Showing posts with the label CHARINDEX

CHARINDEX: Get index of the delimiting space

CHARINDEX: Get index of the delimiting space .Net Framework has very useful String.IndexOf that returns position of first occurrence of given char or substring in string. Although there is no function named IndexOf in T-SQL you can achieve same result with CHARINDEX and PATHINDEX function. CHARINDEX function as counterpart of IndexOf() CHARINDEX function has the following syntax: CHARINDEX(expression1, expression2 [, start_location]) expression1 - sequence of characters to be found expression2 - column or expression that is searched start_location - optional parameter, start position of searching Example that search for first position of word "chart" in ProductDescription column could look like: SELECT CHARINDEX ( 'chart' , ProductDescription ) FROM Products PATINDEX function PATINDEX function works very similar to CHARINDEX. Unlike CHARINDEX, PATINDEX function has only two parameters and first parameter is pattern that can include wildcard p...