[英]extract specific substring sql
I have a database that contains 16 columns. 我有一个包含16列的数据库。 In the 16 column I have the following text:
在第16栏中,我有以下文字:
ASN_MAF=0.09;DOMAINS=Pfam_domain:PF00168,Prints_domain:.
I want to extract PF00168, so the substring between Pfam_domain: and ,. 我想提取PF00168,所以Pfam_domain:和之间的子字符串。 All rows have this pattern: Pfam_domain: and ,.
所有行都具有以下模式:Pfam_domain:和。
I try to do this request but it doesn't work: 我尝试执行此请求,但不起作用:
res = sqldf("
SELECT SUBSTRING(v16, CHARINDEX("Pfam_domain:",v16)+1, 10000), CHARINDEX(",",v16)-1 )
FROM GeminiTable_germ
")
Try: 尝试:
SELECT SUBSTRING(v16, CHARINDEX('Pfam_domain:', v16)+12, CHARINDEX
(',',v16) - (CHARINDEX('Pfam_domain:', v16)+12))
Note that I changed the " to ' inside the SQL statement - that is an important distinction in SQL. 请注意,我在SQL语句中将“”更改为-这是SQL中的重要区别。
Assuming we want the string between colon and comma, instr(v16, ':')+1
is the character position after the colon. 假设我们想要冒号和逗号之间的字符串,则
instr(v16, ':')+1
是冒号之后的字符位置。 Also, the string we want is of length instr(v16, ',') - instr(v16, ':')-1
so use substr
with those 2nd and 3rd arguments: 另外,我们想要的字符串的长度为
instr(v16, ',') - instr(v16, ':')-1
因此substr
与第二和第三参数一起使用:
library(sqldf)
GeminiTable_germ <- data.frame(v16 =
"ASN_MAF=0.09;DOMAINS=Pfam_domain:PF00168,Prints_domain:.")
sqldf("select substr(v16, instr(v16, ':')+1, instr(v16, ',')-instr(v16, ':')-1) v16new
from GeminiTable_germ")
giving: 赠送:
v16new
1 PF00168
We could alternately break it up and write it like this instead: 我们可以将其分解并写成这样:
field <- function(x, from, to) {
from_pos <- sprintf("instr(%s, '%s')+1", x, from)
to_pos <- sprintf("instr(%s, '%s')-%s-2", x, to, from_pos)
sprintf("substr(%s, %s, %s)", x, from_pos, to_pos)
}
field('v16', ':', ',') # view generated code
fn$sqldf("select `field('v16', ':', ',')` v16new from GeminiTable_germ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.