| Author |
Topic |
|
hog
Constraint Violating Yak Guru
284 Posts |
Posted - 2006-02-13 : 11:02:26
|
| Is it possible to efficienlty splice a single column that contains a comma seperated list into a select insert statement?insert into @MyTab (col1, col2, col3)selectleft(text_field, charindex(',', text_field)-1),????????from A_Tablethe left statement works a treat and gives me the correct entry, but col2 and col3 is getting very messing using nested charindex, left and substings and am worried on a large recordset could drag a lot?Thnx |
|
|
hog
Constraint Violating Yak Guru
284 Posts |
Posted - 2006-02-13 : 13:44:32
|
| have found an old post of mine which dealt with this very thing, all sorted :) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-14 : 01:54:27
|
| And what is your solution?MadhivananFailing to plan is Planning to fail |
 |
|
|
hog
Constraint Violating Yak Guru
284 Posts |
Posted - 2006-02-14 : 16:35:08
|
| This works a treat :)UPDATE @MyTabSET @I1 = CHARINDEX(',', note + ',' ), code1 = CASE WHEN @I1-1 > 0 THEN LTRIM(SUBSTRING(note + ',', 1, @I1-1)) ELSE '' END,@I2 = CHARINDEX(',', note + ',' , @I1+1),code2 = CASE WHEN @I2-@I1-1 > 0 THEN LTRIM(SUBSTRING(note + ',', @I1+1, @I2-@I1-1)) ELSE '' END, @I3 = CHARINDEX(',', note + ',' , @I2+1), code3 = CASE WHEN @I3-@I2-1 > 0 THEN LTRIM(SUBSTRING(note + ',', @I2+1, @I3-@I2-1)) ELSE '' END, @I4 = CHARINDEX(',', note + ',', @I3+1),code4 = CASE WHEN @I4-@I3-1 > 0 THEN LTRIM(SUBSTRING(note + ',', @I3+1, @I4-@I3-1)) ELSE '' ENDFROM @MyTabThe case statement caters for non entries. |
 |
|
|
|
|
|