how to select next n letters after one specific letter using function in sql server by giving function name,specific letter, no of letters next

Mixture of CHARINDEX with SUBSTRING if on SQL Server.

DECLARE @Text VARCHAR(100) = 'The cat is under the table.'

DECLARE @CharToSearch CHAR = 's'
DECLARE @CharactersToRetrieve INT = 5

SELECT
    CharacterPosition = CHARINDEX(@CharToSearch, @Text),
    TrimmedString = SUBSTRING(
        @Text,
        CHARINDEX(@CharToSearch, @Text) + 1,
        @CharactersToRetrieve)

Result:

CharacterPosition   TrimmedString
10                   unde

Leave a Comment