Contains two columns of data, "field1" and "field2" Sample data:
Field1: joe|sam|pete|mike
Field2: Manager|Team Lead|Worker Ant|Worker Ant
So you can see a field1 contains multiple names, and Field2 contains
their job titles. Both are separated by the "|" key. I want to merge
these differently into Field3:
Field3 would look like:
joe|Manager|sam|Team Lead|pete|Worker Ant|mike|Worker Ant
So it would based on the pipes, merge the name and title. It would
populate each row in the database based on what's in it's associated
Field1 and Field2 values.
Any help would be appreciated as this is a hot item I need to take
care of.
Thanks.
JR
Thanks for the input Carl. I am working on this now. If you or
anyone has any samples, let me know.
Where I am so far:
DECLARE @txt VARCHAR(100)
SET @txt ='|'
SELECT employees,
(LEN(employees)-LEN(REPLACE(employees,@txt,'')))/LEN(@txt)
FROM people
Probably need to add +1 to this as it gives me one less than I need
however a start. This will get me the number of occurrences.
Thanks.
use CAMPO_NOTES
DEALLOCATE tnames_cursor
DECLARE @people varchar(1000)
DECLARE @position varchar(1000)
DECLARE @id varchar(20)
DECLARE @valuess int
DECLARE @txt VARCHAR(100)
SET @txt = '|>'
DECLARE tnames_cursor CURSOR
FOR
SELECT id,staff,posizione, ((LEN(staff)-
LEN(REPLACE(staff,@txt,'')))/LEN(@txt)) as valuess
FROM EXP
OPEN tnames_cursor
FETCH NEXT FROM tnames_cursor INTO @id,@people,@position,@valuess
WHILE (@@FETCH_STATUS <> -1)
BEGIN
print @valuess
if (@valuess > 0)
BEGIN
WHILE (@valuess > 0)
BEGIN
print LEFT(@people, CHARINDEX('|>', @people) - 1)
SET @people = right(@people, len(@people) - CHARINDEX('|>',
@people) - 1)
print LEFT(@position, CHARINDEX('|>', @position) - 1)
SET @position = right(@position, len(@position) - CHARINDEX('|
>', @position) - 1)
SET @valuess = @valuess - 1
END
END
ELSE
BEGIN
print @people
print @position
END
print '----------------------------------------------------'
FETCH NEXT FROM tnames_cursor INTO @id,@people,@position,@valuess
END
CLOSE tnames_cursor
DEALLOCATE tnames_cursor
--DROP TABLE #piped_values
GO
CREATE TABLE #piped_values( pair_id INT, field_name INT, initial_value
VARCHAR(80) ) --
GO
--DROP TABLE #concat_values
GO
CREATE TABLE #concat_values( pair_id INT PRIMARY KEY, field_name INT, pos1
INT, pos2 INT, concat_value VARCHAR(400) )
GO
SET NOCOUNT ON
INSERT INTO #piped_values VALUES ( 1, 1, 'joe|sam|pete|mike' )
INSERT INTO #piped_values VALUES ( 1, 2, 'Manager|Team Lead|Worker
Ant|Worker Ant' )
INSERT INTO #piped_values VALUES ( 2, 1, 'rita|sue|bob|too' )
INSERT INTO #piped_values VALUES ( 2, 2, 'DBA|Extra|Hobbyist|Database
Development Professional' )
GO
-- Add first name value
INSERT INTO #concat_values ( pair_id, field_name, pos1, pos2, concat_value )
SELECT pair_id, field_name, CHARINDEX( '|', initial_value ), 1, LEFT(
initial_value, CHARINDEX( '|', initial_value ) )
FROM #piped_values
WHERE field_name = 1
GO
DECLARE @i INT
SET @i = 1
WHILE @i < 8
BEGIN
-- Flick between adding title or name
IF @i % 2 = 1
BEGIN
-- Add next title value
UPDATE cv
SET concat_value = concat_value + SUBSTRING( initial_value, cv.pos2,
ISNULL( NULLIF( CHARINDEX( '|', initial_value , cv.pos2 + 1 ), 0 ) - cv.pos2,
LEN( initial_value ) ) ),
pos2 = CHARINDEX( '|', initial_value, cv.pos2 + 1 )
FROM #piped_values pv
INNER JOIN #concat_values cv ON pv.pair_id = cv.pair_id
WHERE pv.field_name = 2
END
ELSE
BEGIN
-- Add next name value
UPDATE cv
SET concat_value = concat_value + SUBSTRING( initial_value, cv.pos1,
ISNULL( NULLIF( CHARINDEX( '|', initial_value, cv.pos1 + 1 ), 0 ) - cv.pos1,
LEN( initial_value ) ) ),
pos1 = CHARINDEX( '|', initial_value, cv.pos1 + 1 )
FROM #piped_values pv
INNER JOIN #concat_values cv ON pv.pair_id = cv.pair_id
WHERE pv.field_name = 1
END
SET @i = @i + 1
-- Exit loop if there's no more work to do
-- !!TODO - one for the user to do!!
END
SELECT 'Finish', @i, * FROM #concat_values
GO
SET NOCOUNT OFF
http://beyondsql.blogspot.com/2007/10/dataphor-merging-strings-together.html
best,
steve
www.beyondsql.blogspot.com