Results 1 to 8 of 8

Thread: Split a string

  1. #1
    Join Date
    Nov 2004
    Posts
    51

    Unanswered: Split a string

    Hi
    I have this string which might have a hyphen in it "-"
    What I want to do is
    if I get a hyphen then take all characters after hyphen
    else take only all the characters starting from the 5th position of the string
    How can this be achieved?

  2. #2
    Join Date
    Nov 2002
    Posts
    272
    You need Case, CharIndex and SubString. Look them up in Books OnLine and I'm sure a Superprogrammer like you can figure it out

  3. #3
    Join Date
    Nov 2004
    Posts
    51
    Thanks
    Only thing i could not figure out is how to go from a certain position till the end of the string
    For instance......in the string "United States"
    I want to go from 5th character till end..ie. "ed States"
    how can i give second paramater in the substring here

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    SUBSTRING( 'United States', 5, LEN('United States')-4 )
    Last edited by r937; 10-29-07 at 12:15.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Nov 2002
    Posts
    272
    Oh, that's right. I thought Substring would return everything from the starting position to the end of the string if the third argument was omitted.
    Different language probably...

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    The third parameter doesn't look very omitted to me.

    FWIW, you can also use an overly large bogus value for the third parameter. It will do no harm.

    -PatP

  7. #7
    Join Date
    Nov 2004
    Posts
    51
    Quote Originally Posted by Pat Phelan
    The third parameter doesn't look very omitted to me.

    FWIW, you can also use an overly large bogus value for the third parameter. It will do no harm.

    -PatP
    Well, thats right but it will fill the extra characters with spaces
    Anyways, thanks
    SUBSTRING( 'United States', 5, LEN('United States')-5 )
    solves my problem!

  8. #8
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    please note, i corrected my post, it's LEN - 4
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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