Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    6

    Unanswered: split text field in SQL SErver 2008

    In SQL SErver 2008, I have a text column. I need to display either 2nd word in the text column or 1st word in the text column based on certain conditions.

    How shall i display either 2nd word or 1st word from a text field.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'm going to assume you mean a varchar field and if so this should get you started
    Code:
    SELECT your_field
         , CharIndex(' ', your_field) As first_space
         , CharIndex(' ', your_field, CharIndex(' ', your_field) + 1) As second_space
    FROM   your_table
    ^ UNTESTED CODE ^

    Have a sit and think about how this can help you, remembering that you might not always have a first, or even second space to search upon.

    Clues: Coalesce(), Len(), SubString()
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •