473,416 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Split one field string with a delimitter into multiple fields

code green
1,726 Expert 1GB
I am migrating data from DB1 to DB2 via csv.
In DB1 there is only one address field 'address' that contains carriage returns between each address line.
DB2 however has three fields for the address going into the csv as Addr-01, Addr-02, Addr-03
How do I split this one field into three fields?
It has to be three even though DB1 address may have four or five 'lines' so any extra will just have to be concatenated into address3.

This is roughly the query
Expand|Select|Wrap|Line Numbers
  1. SELECT customer 'Account',
  2. full_name Name, address 'Addr-01',
  3.     --Addr-02,
  4.     --Addr-03,
  5. city 'Town',
  6. county 'County',
  7. postcode 'Post Code' FROM customer
I wrote a function that returned a table before I found it had to be used in a JOIN which meant a record for each address line. This is the function
FUNCTION splitString (@delim CHAR(1), @str VARCHAR (100))
RETURNS @t TABLE (line VARCHAR(50))
AS
BEGIN
DECLARE @pos INT
DECLARE @piece VARCHAR(50)

IF RIGHT(RTRIM(@str),1) <> @delim
SET @str = @str + @delim

SET @pos = CHARINDEX(@delim, @str)
WHILE @pos <> 0
BEGIN
SET @piece = LEFT(@str, @pos - 1)

INSERT INTO @t VALUES(CAST(@piece AS VARCHAR(50)))

SET @str = STUFF(@str, 1, @pos, '')
SET @pos = CHARINDEX(@delim , @str)
END
RETURN
END
Apr 1 '10 #1
1 4124
The function below may get what you are after, it works on a very limited test I have done. There is no error trapping in the function so if there are not enough carriage returns (i.e. at least 2) it may have a fit!:

Expand|Select|Wrap|Line Numbers
  1. FUNCTION MySplit(@Line int, @str nvarchar(255))
  2. RETURNS nvarchar(255)
  3.  
  4. AS
  5. BEGIN
  6.  
  7. DECLARE    @Output nvarchar(255)
  8.  
  9. SET @Output = CASE    @Line
  10.                 WHEN 1 THEN    substring(@Str,1,CHARINDEX(char(13), @str))
  11.                 WHEN 2 THEN substring(@Str,CHARINDEX(char(13), @str)+1,charindex(char(13),(substring(@str,charindex(char(13),@str)+1,255))))
  12.                 WHEN 3 THEN substring(@Str,
  13.                         len(substring(@Str,1,CHARINDEX(char(13), @str)))
  14.                         + 2 +
  15.                         len(substring(@Str,CHARINDEX(char(13), @str)+1,charindex(char(13),(substring(@str,charindex(char(13),@str)+1,255))))),
  16.                         255)
  17.                 ELSE    'Not defined'
  18.             END
  19.  
  20. RETURN @Output
  21. END
you would then need to call the function 3 times passing the line variable you are after:

Expand|Select|Wrap|Line Numbers
  1. SELECT   MySplit(1,Address1) AddressLine1
  2.         ,MySplit(2,Address1) AddressLine2
  3.         ,MySplit(3,Address1) AddressLine3
  4.         ,MySplit(4,Address1) JustTest
  5.         ,Address1
  6. FROM        MyTable
Jun 2 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
5
by: bu | last post by:
I have a database field that represents an item description and varying length. I am using the database to import the item description into another program (the other program is ODBC-aware ). The...
2
by: ThurstonHowl | last post by:
Hello, my task is the following: Input are tables with fields containing strings where the strings are actually delimited lists. For example, one field could contain 'AB|CD|EF|GH' I've...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
8
by: J Stoodley | last post by:
I am in a learning curve right now, and want to become well aquanted with VB.NET. So, I have two questions. 1 is technial the other is resource related. 1. I need to strip a single character...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
4
by: kaplan.gillian | last post by:
Hi everyone, I currently have an Access database that includes quite a few long memo fields. When I create a report of my data, Access does not allow the memo fields to be split with the page...
1
code green
by: code green | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.