select string from []

  • Hello,

    I do have table with column A and In column A varchar(100) .. I have value like

    Column A

    Hello my name is xyz [please tell me your school]

    In stored procedure, I would like to return only "please tell me your school".

    I would like to pick up string from "[".

    Can any one help me to write sql script.

    Thanks

    LP

  • I'm including the code to retrieve the text between [] and avoid rows that won't contain a proper format. Feel free to modify it as needed and ask any questions you have.

    DECLARE @String varchar(8000) = 'Hello my name is xyz [please tell me your school]'

    SELECT SUBSTRING(@String, CHARINDEX('[', @String) + 1, CHARINDEX(']', @String) - CHARINDEX('[', @String) - 1)

    WHERE CHARINDEX('[', @String) < CHARINDEX(']', @String)

    AND CHARINDEX('[', @String) > 0

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you so much

Viewing 3 posts - 1 through 2 (of 2 total)

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