[英]Extracting nth field of string delimited by “:” stored in a SQL column
I have a SQL table with the two following columns: 我有一个包含以下两列的SQL表:
FORMAT Sample
GT:AD:DP:GQ:PL 0/0:233,0:233:99:0,120,1800
GT:AD:DP:GQ:PL 0/1:101,61:220:99:835,0,1859
GT:AD:DP:GQ:PL 0/0:172,0:172:99:0,120,1800
GT:AD:DP:GQ:PL 0/0:216,0:216:99:0,120,1800
GT:AD:DP:GQ:PL 0/0:216,0:216:99:0,120,1800
GT:AD:DP:GQ:PGT:PID:PL 0/1:185,232:417:99:0|1:8029494_T_G:8670,0,6429
GT:AD:DP:GQ:PL 0/0:367,0:367:99:0,120,1800
GT:AD:DP:GQ:PGT:PID:PL 0/1:150,198:348:99:0|1:8029494_T_G:7930,0,5677
GT:AD:DP:GQ:PGT:PID:PL 0/1:148,196:344:99:0|1:8029494_T_G:7876,0,5652
GT:AD:DP:GQ:PGT:PID:PL 0/0:148,0:344:99:0|1:8029494_T_G:7876,8334,14591
GT:AD:DP:GQ:PGT:PID:PL 0/0:148,0:344:99:0|1:8029494_T_G:7876,8334,14591
The FORMAT column specifies the IDs for the fields that are given in the following column splitted by ":". FORMAT列指定以下列中用“:”分隔的字段的ID。
I would like to extract specific fields from the second column based on the ID/position from the FORMAT column, ie AD (2nd), DP (3rd) or GQ (4th). 我想根据FORMAT列中的ID /位置从第二列中提取特定字段,即AD(第二),DP(第三)或GQ(第四)。
I was able to extract the AD field with the following code: 我能够使用以下代码提取AD字段:
SELECT SUBSTRING(Sample, CHARINDEX(':',Sample)+1, CHARINDEX(':',Sample,5)-5) FROM Table 1;
The problem is that I am not able to extract the fields DP or GQ, since the length of the different fields is not always the same one and I cannot specify which should be the starting position to search for the following ":" location. 问题是我无法提取DP或GQ字段,因为不同字段的长度并不总是相同,并且我无法指定哪个位置应该是搜索以下“:”位置的起始位置。
I also tried to use the Split function from this website: 我也尝试从此网站使用分割功能:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
The problem is that I do not know how to declare a column as a variable so that I can extract the required field for every single row of the table. 问题是我不知道如何将一列声明为变量,以便为表的每一行提取必需的字段。
The desired output for the [Sample] column should look like this: [Sample]列的所需输出应如下所示:
GT AD DP GQ
0/0 233,0 233 99
0/1 101,61 220 99
0/0 172,0 172 99
0/0 216,0 216 99
0/0 216,0 216 99
0/1 185,232 417 99
0/0 367,0 367 99
0/1 150,198 348 99
0/1 148,196 344 99
0/0 148,0 344 99
0/0 148,0 344 99
Any help would be appreciated, 任何帮助,将不胜感激,
Thanks, 谢谢,
Perhaps a little XML as the parser 也许有一些XML作为解析器
Example 例
Select A.Format
,B.*
From YourTable A
Cross Apply (
Select Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
,Pos3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
,Pos4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
From (Select Cast('<x>' + replace((Select replace(A.Format,':','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A
) B
Returns 返回
Format Pos2 Pos3 Pos4
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PGT:PID:PL AD DP GQ
GT:AD:DP:GQ:PL AD DP GQ
GT:AD:DP:GQ:PGT:PID:PL AD DP GQ
GT:AD:DP:GQ:PGT:PID:PL AD DP GQ
GT:AD:DP:GQ:PGT:PID:PL AD DP GQ
GT:AD:DP:GQ:PGT:PID:PL AD DP GQ
Or a Simple version 或简单版本
Select A.Format
,Pos2 = Cast('<x>' + replace(Format,':','</x><x>')+'</x>' as xml).value('/x[2]','varchar(max)')
,Pos3 = Cast('<x>' + replace(Format,':','</x><x>')+'</x>' as xml).value('/x[3]','varchar(max)')
,Pos4 = Cast('<x>' + replace(Format,':','</x><x>')+'</x>' as xml).value('/x[4]','varchar(max)')
From YourTable A
Or if Open to a UDF 或者,如果打开UDF
Take a peek at TSQL/SQL Server - table function to parse/split delimited string to multiple/separate columns 看看TSQL / SQL Server-表函数将分隔的字符串解析/拆分为多个/分离的列
EDIT - Update for Sample
编辑-更新样本
Select A.Format
,GT = Cast('<x>' + replace(Sample,':','</x><x>')+'</x>' as xml).value('/x[1]','varchar(max)')
,AD = Cast('<x>' + replace(Sample,':','</x><x>')+'</x>' as xml).value('/x[2]','varchar(max)')
,DP = Cast('<x>' + replace(Sample,':','</x><x>')+'</x>' as xml).value('/x[3]','varchar(max)')
,GQ = Cast('<x>' + replace(Sample,':','</x><x>')+'</x>' as xml).value('/x[4]','varchar(max)')
From YourTable A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.