Split a string where newline, carriage return

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Split a string where newline, carriage return

    I have an address field in a table that contains one, two, three or more lines of an address.
    I need to split this into three fields for later insertion into CSV.

    I have done something similar with the name field which I don't lile because I am testing the same condition twice
    Code:
    CASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN 
    RTRIM(LTRIM(SUBSTRING(full_name,CHARINDEX(' ',full_name)+1,20)))
    ELSE RTRIM(LTRIM(full_name)) 
    END surname,
    CASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN 
    RTRIM(LTRIM(SUBSTRING(full_name,1,CHARINDEX(' ',full_name)-1)))
    ELSE RTRIM(LTRIM(initials))
    END	forename,
    but the delimiter here was always space.
    The address field has carriage return, newline and possibly tab.
    How do I split the string where any of these delimiters occur?

    (The delimitter is showing as an empty square)
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    I think
    charindex( fieldtotest,cha r(13) ) will give the position of carriage return
    charindex( fieldtotest,cha r(10) ) will give the position of line feeds

    I can't find anything in my data to test the theory on

    Comment

    Working...