My problem is: I have a varchar
-type column (IDs) with string records like this: "123056; 96445; 123100; 45893; 201971" and I want to divide them into the next fields:
ID_1 | ID_2 | ID_3 | ID_4 | ID_5
123056 | 96445 | 123100 | 45893 | 201971
Actually, I used this code:
update MyTable
set ID_1 = IDs
and then:
update MYTable
set ID_2 =
SUBSTRING(ID_1, CHARINDEX (';', ID_1)+2, LEN(ID_1))
where ID_1 like '%;%'
update MYTable
set FRS_ID_1 =
SUBSTRING(FRS_ID_1, 1, CHARINDEX (';', FRS_ID_1)-1)
where ID_1 like '%;%'
The ID_1 like '%;%'
is necessary because some fields contain only one number not a list. So I ran this code five times, manually updated the column name from ID_1
to ID_2
and etc. but it's not professional and very slow.
How could I solve it with an iteration or in another way?
Thank you.