我的初始字符串是
Content-Disposition: attachment; filename="0001.zam"
我想选择两个“字符之间的所有内容(本例中为”0001.zam“)。我知道我需要使用类似于的SUBSTRING和CHARINDEX函数:
SELECT SUBSTRING(@Attachment, CHARINDEX('"', @Attachment),...)
我无法弄清楚要传递的内容是第二个SUBSTRING参数。请注意,两个“字符和第二个字符后面的字符串”之间的字符串是可变的。整个字符串可以看起来像。像这样:
Content-Disposition: attachment; filename="0001556.txt"; size=187;
最重要的是获得两个“角色”之间的所有内容。
答案 0 :(得分:3)
获取您希望它使用left()
和right()
函数的数据的另一种方法。
select left(right(t, len(t)- CHARINDEX('"', t)), charindex('"',right(t, len(t)- CHARINDEX('"', t)))-1)
from
(
select 'Content-Disposition: attachment; filename="0001.zam"' t
) u
此输出
0001.zam
我希望,而不是假设,此标题中只有两个“。
答案 1 :(得分:2)
SQL2016 +:
SELECT *
FROM (VALUES ('Content-Disposition: attachment; filename="0001556.txt"; size=187;')) x(SourceString)
OUTER APPLY (
SELECT SUBSTRING(split.value, CHARINDEX('=', split.value) + 1, 8000) FileName
FROM STRING_SPLIT(x.SourceString, ';') split
WHERE split.value LIKE ' filename=%'
) y
结果:
答案 2 :(得分:1)
您在开始时尝试使用bash (1)
的解决方案:
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length
characters of the value of parameter starting at the
character specified by offset. [...] If length is
omitted, expands to the substring of the value of
parameter starting at the character specified by
offset and extending to the end of the value. length
and offset are arithmetic expressions (see ARITHMETIC
EVALUATION below).