Split table value into multipe columns and create a column name as well
For a Database in SQL server, I have data for one column that looks like this
Severity
10006=0;10007=2;10008=5;10009=1;
10006=0;10007=开发者_运维技巧1;10008=6;10009=0;
10006=0;10007=3;10008=5;10009=1;
- 10006 = Critical
- 10007 = Major
- 10008 = Minor
- 10009 = Trivial
Is there a way to create a function where I can select columnName. and have the output match this result.
critical Major Minor Trivial
--------------------------------
0 2 5 1
0 1 6 0
0 3 5 1
This works for the data
with t as (
select '10006=0;10007=2;10008=5;10009=1;' as col union
select '10006=0;10007=1;10008=6;10009=0;' union
select '10006=0;10007=3;10008=5;10009=1;')
select substring(col,patindex('%10006=[0-9];%',col)+6,1) as critical,
substring(col,patindex('%10007=[0-9];%',col)+6,1) as major,
substring(col,patindex('%10008=[0-9];%',col)+6,1) as minor,
substring(col,patindex('%10009=[0-9];%',col)+6,1) as trivial
from t
精彩评论