Connecting Tech Pros Worldwide Forums | Help | Site Map

Split a string where newline, carriage return

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#1: Sep 24 '09
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
Expand|Select|Wrap|Line Numbers
  1. CASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN 
  2. RTRIM(LTRIM(SUBSTRING(full_name,CHARINDEX(' ',full_name)+1,20)))
  3. ELSE RTRIM(LTRIM(full_name)) 
  4. END surname,
  5. CASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN 
  6. RTRIM(LTRIM(SUBSTRING(full_name,1,CHARINDEX(' ',full_name)-1)))
  7. ELSE RTRIM(LTRIM(initials))
  8. 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's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 790
#2: Sep 25 '09

re: Split a string where newline, carriage return


I think
charindex( fieldtotest,char(13) ) will give the position of carriage return
charindex( fieldtotest,char(10) ) will give the position of line feeds

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