Strip out part of a text field in sql 2008 to leave the required part only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HelpIsNeeded
    New Member
    • Oct 2013
    • 1

    Strip out part of a text field in sql 2008 to leave the required part only

    How can you remove a part of a text field in sql but leave the section that is needed such as
    Code:
    the cat went up the hill: the dog followed on behind    the mouse went the other way
    (4 spaces between 'behind' and 'the mouse')

    'the cat followed on behind' is the only bit I need

    Also need to add that the text field varies in length so need to use ':' and ' the mouse' to identify the beginning and end
    Last edited by Frinavale; Oct 30 '13, 06:28 PM. Reason: Added code tags so that the format appears properly
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can use the CHARINDEX function to find the location of those strings and the SUBSTRING function to extract the parts you need.

    Comment

    • SimMed
      New Member
      • Oct 2013
      • 2

      #3
      Code:
      DECLARE @X NVARCHAR(MAX) = 'the cat went up the hill: the dog followed on behind the mouse went the other way'
       SELECT SUBSTRING(@X,0,8)+ SUBSTRING(@X,33,20)+ ': '+SUBSTRING(@X,55,9)
      Something like that?
      Last edited by Rabbit; Oct 30 '13, 05:47 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

      Comment

      Working...