[英]SQL Server: How to use substring in a query?
i have this kind of row: 我有这样的行:
[Arturo Ochando] <20>
But i want only: 但我只想要:
Arturo Ochando
How can i do that ? 我怎样才能做到这一点 ?
And how use this in a 'select' operation ? 以及如何在“选择”操作中使用它?
update: i would like to find the first and the last '[' ']' and catch only what is inside there. 更新:我想找到第一个和最后一个'['']'并只捕获其中的内容。
Example: 例:
voice: English version) [Cobalt Claw]
声音:英文版)[钴爪]
return 返回
Cobalt Claw
钴爪
Best regards, Valter Henrique. 最好的问候,Valter Henrique。
Get text between first [
and next ]
. 在first
[
和next ]
之间获取文本。
-- cte for test data
;with actor_character(character) AS
(
select 'voice: English version) [Cobalt Claw]' union all
select 'voice: English version) [Cobalt Claw' union all
select 'voice: English version) Cobalt Claw]' union all
select 'voice: English version) ]Cobalt Claw[' union all
select 'voice: English version) Cobalt Claw'
)
select *,
case
-- Test for not valid positions
when Start.Pos = 1 or Stop.Pos = 0
then character
else substring(character, Start.Pos, Stop.Pos-Start.Pos)
end
from actor_character
cross apply (select charindex('[', character)+1) as Start(Pos)
cross apply (select charindex(']', character, Start.Pos)) as Stop(Pos)
Get text between first [
and last ]
. 获取first
[
和last ]
之间的文本。
-- cte for test data
;with actor_character(character) AS
(
select 'voice: English version) [Cobalt Claw]' union all
select 'voice: English version) [Cobalt Claw' union all
select 'voice: English version) Cobalt Claw]' union all
select 'voice: English version) ]Cobalt Claw[' union all
select 'voice: English version) [Cobalt]Claw]' union all
select 'voice: English version) Cobalt Claw'
)
select *,
case
-- Test for not valid positions
when Start.Pos = 0 or Stop.Pos = 0 or Start.Pos > len(character)-Stop.Pos
then character
else substring(character, Start.Pos+1, len(character)-Stop.Pos-Start.Pos)
end
from actor_character
cross apply (select charindex('[', character)) as Start(Pos)
cross apply (select charindex(']', reverse(character))) as Stop(Pos)
It sounds like you need a regular expression, to get the data you need out of your source string. 听起来您需要一个正则表达式,才能从源字符串中获取所需的数据。
http://justgeeks.blogspot.com/2008/08/adding-regular-expressions-regex-to-sql.html http://justgeeks.blogspot.com/2008/08/adding-regular-expressions-regex-to-sql.html
http://blog.tech-cats.com/2007/09/using-regular-expression-in-sql-server.html http://blog.tech-cats.com/2007/09/using-regular-expression-in-sql-server.html
select substring(field, charindex('[', field) + 1, charindex(']', field) - charindex('[', field) - 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.