I have a table that looks like follows:
NAME(varchar(6), STRING(varchar(250)
ABCD '1 2 1 173 1 8 9 1 1 2 4 7 1 3.....'
APLC '1 3 11 34 1 4 99 33 23 111 12 6 7 8....'
the string continues with this up to 250 characters.
What I am trying to do is get the values and there respective positions from this string.
I know I can use Charindex but that gives me only the first position of a number in the string.
e.g.
Select Charindex ('2',STRING) where Name = ABCD
ANSWER = 7
But what I’m looking for is something like a table that have the following in for each Name
Name Position Value
---------------------------
ABCD, 7, 2
ABCD, 1, 1
ABCD, 13, 1
ABCD, 18, 1
ABCD, 19, 7
Any ideas welcome 🙂
With a little help of a numbers table it could look like this.
Working sample with table variable and
master..spt_valuesas a numbers table.