1

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.

3 Answers 3

2

Get text between first [ and 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 ].

-- 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)
Sign up to request clarification or add additional context in comments.

3 Comments

Nice: How would you update this to find the last ']' (See the update in the question)?
@Jon – Added a version to use last ] instead.
Would I be able to take a column in the query and get a substring of that column in the same select statement?
1

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://blog.tech-cats.com/2007/09/using-regular-expression-in-sql-server.html

Comments

0
select substring(field, charindex('[', field) + 1, charindex(']', field) - charindex('[', field) - 1)

8 Comments

@squawknull this <20> came together with the row, the database was not mine.
I see. So it's part of the string. I'll revise my query.
i try what you recommend, but now working here's my case: SELECT TOP (50) substring(character, charindex('[', character) + 1, charindex(']', character) - charindex('[', character) - 1) FROM actor_character
returns:Invalid length parameter passed to the LEFT or SUBSTRING function. Severity 16 State 2
Does every single row have both the "[" and "]" characters?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.