create function

  • HI,

    I need to create a function that receives a parameter (string) and returns a value if there is a second '*' in the string.

    E.g:

    Function receives the string:

    *4112*21211222

    returns number 6

    Other e.g

    Function receives the string:

    *411221211*222

    returns number 11

    Other e.g

    Function receives the string:

    411221211222

    returns nothing, because there is no second *

    Can someone help me with the t-sql to do this?

    Thnak you

  • declare @SearchString varchar(100)

    set @SearchString = '*411221211*222'

    select charindex('*',@SearchString,CHARINDEX('*',@SearchString,1)+1)

  • IF LEN(@string) - LEN(REPLACE(@string, '*', '')) >= 2

    BEGIN

    RETURN LEN(@string) - CHARINDEX('*', REVERSE(@string)) + 1

    END

  • Use this:

    Select CHARINDEX('*',@InputString,CHARINDEX('*',@InputString, 1) + 1)

    Returns 0 if it doesn't find a second '*'.

    edit: too late 🙁

  • thank you very much

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply