| Author |
Topic |
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 06:29:12
|
| HiI have a field (field1) in a table which contains a list of number seperated by commas.:-12345, 23456, 3434488, 77656554578, 12322I have another table which has a field (field2) which contains an instance of one of the (record) numbers in field1 above. e.g it may contain the number 12345 from the list above.I need to pull the numbers out in the order they appear in the field and then extract any records related to each number found in another table.Can anyone help please? |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-27 : 06:39:14
|
So many posts about this on this site. Have you searched for answers yet.Here's one way.IF EXISTS ( SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[fn_Split]') AND xtype IN (N'FN', N'IF', N'TF') ) DROP FUNCTION [dbo].[fn_Split]GOCREATE FUNCTION fn_Split (@text VARCHAR(8000), @delimiter VARCHAR(20) = ' ') RETURNS @Strings TABLE ( position INT IDENTITY PRIMARY KEY , value VARCHAR(8000) )AS BEGIN DECLARE @index int SET @index = -1 WHILE (LEN(@text) > 0) BEGIN -- Find the first delimiter SET @index = CHARINDEX(@delimiter , @text) -- No delimiter left? -- Insert the remaining @text and break the loop IF (@index = 0) AND (LEN(@text) > 0) BEGIN INSERT INTO @Strings VALUES (@text) BREAK END -- Found a delimiter -- Insert left of the delimiter and truncate the @text IF (@index > 1) BEGIN INSERT INTO @Strings VALUES (LEFT(@text, @index - 1)) SET @text = RIGHT(@text, (LEN(@text) - @index)) END -- Delimiter is 1st position = no @text to insert ELSE SET @text = RIGHT(@text, (LEN(@text) - @index)) END RETURNENDGOSELECT * FROM dbo.fn_split('123,12,12,55546,2341,231,23123', ',')-------------Charlie |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 06:39:49
|
may be thisSELECT *FROM table1 t1INNER JOIN table2 t2ON ','+t1.field1+',' LIKE '%,' + CAST(t2.field2 AS varchar(10))+ ',%' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 06:45:36
|
or this:-SELECT t2.*FROM table2 t2INNER JOIN (SELECT t.field1,b.ID,b.Val FROM table1 t1 CROSS APPLY dbo.ParseValues(t1.field1) b )t1ON t1.Val=t2.field2ORDER BY t1.ID ParseValues can be found in below linkhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=104485 |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 06:53:02
|
| Sorry and thanks all |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 07:04:24
|
quote: Originally posted by Bill_C Sorry and thanks all
welcome |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 07:37:15
|
| Sorry , another quick oneWould that function work if I wanted to pull out the whole row for every instance of a sequence found in that field?e.gfiled1 = 12345, 23456, 3344556Then in the same table:-filed2 = 'Mr'field3 = 'Johnson'field4 = 'hello'And say i wanted the output to read:-field1 - field2 - field3 - field4 12345 - Mr - Johnson - hello23456 - Mr - Johnson - hello3344556 - Mr - Johnson - hello |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 07:42:08
|
| on what basis you'll link field2,3,4... to field1's value?is there another linking field? |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 07:53:44
|
| Your function does split them properley, SELECT t.*,b.Val FROM dbo.tablename tCROSS APPLY ParseValues(t.fieldname)bbut how do I get a counter for each instance of the number sequences found?e.g. counter - field1 - field2 - field3 - field4 1 - 12345 - Mr - Johnson - hello 2 - 23456 - Mr - Johnson - hello 3 - 3344556 - Mr - Johnson - hello |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 07:55:28
|
| at the moment it comes out like this:EncounterRecNo - field1 - field2 - field3 - field41 - 12345 - Mr - Johnson - hello1 - 23456 - Mr - Johnson - hello1 - 3344556 - Mr - Johnson - hello |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 07:58:46
|
| on what relation do you link the other fields to this? |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 08:08:06
|
| I now have a table with all fields in one row, and I pull out the row once for each sequence of numbers found in field1.So i now have a table with data as follows:-field1 = 12345, 23456, 3344556filed2 = 'Mr'field3 = 'Johnson'field4 = 'hello'and I need to split field1 up and display the whole row (field1, 2,3 & 4) once for each time a number exists in field1EncounterRecNo - field1 - field2 - field3 - field41 - 12345 - Mr - Johnson - hello2 - 23456 - Mr - Johnson - hello3 - 3344556 - Mr - Johnson - helloI can see in the function that there is an identity for ID, and i need output like above?Thanks for your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 08:10:45
|
just do thisSELECT b.ID,b.Val,t.field2,t.field3,t.field4FROM table tCROSS APPLY dbo.ParseValues(t.field1) b |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-10-27 : 08:12:01
|
| Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 08:18:17
|
| [code]declare @Test table(field1 varchar(200),field2 varchar(20), field3 varchar(20), field4 varchar(20) )insert into @testselect '12345, 23456, 3344556','Mr','Johnson','hello'select id,Val,field2,field3,field4from @test tcross apply dbo.parsevalues(t.field1)boutput------------------------------id Val field2 field3 field4----------- ---------- -------------------- -------------------- --------------------1 12345 Mr Johnson hello2 23456 Mr Johnson hello3 3344556 Mr Johnson hello[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 08:18:38
|
Cheers |
 |
|
|
|