SQL Server Substring with CharIndex
https://www.sqlservercurry.com/2016/08/sql-server-substring-with-charindex_7.html
select charindex ('C','ABCDEF',1) -- Returns 3 select charindex ('C','ABCDEF') -- Still returns 3 as start_position is option, default is 1 select charindex ('c','ABCDEF',1) -- By default not case sensitive, so returns 3 select charindex ('c','ABCDEF' COLLATE Latin1_General_CS_AS) –- Returns 0 as lowercase c is not found. select charindex ('Z','ABCDEF',1) -- Returns 0 as there is no Z character select charindex... To find the position of a character or a substring in a string, use the CHARINDEX () function. For example, CHARINDEX ('C', 'ABCDEF') returns 3. To extract a substring from a string, use the SUBSTRING () function. For example, SUBSTRING ('Customer', 2, 3) returns 'ust'. To combine the CHARINDEX () and SUBSTRING () functions, use the position returned by CHARINDEX () as the starting point for SUBSTRING (). For example, SUBSTRING ('Customer', CHARINDEX ('t', 'Customer') + 1, 2) returns 'er'. To avoid repeating the use of CHARINDEX () in a query, use the CROSS APPLY operator to assign an alias to the position. For example, CROSS APPLY (SELECT CHARINDEX (' ', M_COMMENT)) ca (pos1).
To find the position of a character or a substring in a string, use the CHARINDEX () function. For example, CHARINDEX ('C', 'ABCDEF') returns 3.
To extract a substring from a string, use the SUBSTRING () function. For example, SUBSTRING ('Customer', 2, 3) returns 'ust'.
To combine the CHARINDEX () and SUBSTRING () functions, use the position returned by CHARINDEX () as the starting point for SUBSTRING (). For example, SUBSTRING ('Customer', CHARINDEX ('t', 'Customer') + 1, 2) returns 'er'.
To avoid repeating the use of CHARINDEX () in a query, use the CROSS APPLY operator to assign an alias to the position. For example, CROSS APPLY (SELECT CHARINDEX (' ', M_COMMENT)) ca (pos1).
DA: 42 PA: 4 MOZ Rank: 6