I'm having trouble figuring out how to split a field up and create columns with those values in a new table.
Here is an example of what my table looks like
Basically, inside of the keyhash column there are multiple values in there separated by carrots that I need to turn into their own columns in a new table. I don't really know how to get started on accomplishing that.
CodePudding user response:
You can try something like this to split a column of string to multiple columns.
PARSENAME(object_name nvarchar(128),object_part int)
receives a string with '.'
delimeter and can return the nth part of that string
select
tableNumber,
currVer,
KeyAnalysis,
Replace(REPLACE(keyhash,'<',''),'>','.') as KeyHash,
REVERSE(PARSENAME(REVERSE(Replace(REPLACE(keyhash,'<',''),'>','.')), 1)) AS [Column1]
, REVERSE(PARSENAME(REVERSE(Replace(REPLACE(keyhash,'<',''),'>','.')), 2)) AS [Column2]
, REVERSE(PARSENAME(REVERSE(Replace(REPLACE(keyhash,'<',''),'>','.')), 3)) AS [Column3]
from SOF
the results will be like this
tableNumber | currVer | KeyAnalysis | KeyHash | Column1 | Column2 | Column3 |
---|---|---|---|---|---|---|
#(LC) | 21.0401 | Key is: Region row header then subregion if available | 704.Lessors Liability per $100 of limit of ins. | 704 | Lessors Liability per $100 of limit of ins | NULL |
#(LC) | 21.0401 | Key is: Region row header then subregion if available | 704.Occ Liability per $1000 of Annual Gross Sales.Fountain Hills. | 704 | Occ Liability per $1000 of Annual Gross Sales | Fountain Hills |
#(LC) | 21.0401 | Key is: Region row header then subregion if available | 704.Occ Liability per $1000 of Annual Gross Sales. | 704 | Occ Liability per $1000 of Annual Gross Sales | NULL |