Query to catch word after given character

Asked

Viewed 32,475 times

2

Good morning, I need a way to get all the rest of a string after a certain character, for example, I have the following database Base de dados

I always need to find what’s left of the string after the last "»" that is, for the first line I needed to pick up all "Access to the system" because there is no "»"

On the second Row I need to pick up "Farmácia Popular" for it is after the ULTIMO "»"

So far I have the following query

select TOP 10 COUNT(SolID) as soma, SolCaminho as caminho
from Solicitacao where
DATEPART(m, SolData) = DATEPART(m, DATEADD(m, 0, getdate()))
AND DATEPART(yyyy, SolData) = DATEPART(yyyy, DATEADD(m, 0, getdate()))
and UsuIDGrupoRespConclusao = 2655
group by SolCaminho order by soma desc

I tried to do with SUBSTRING with Charindex but did not succeed.

  • Use substring and charindex right that works

  • Could you give more details about the structure of your database? How is this table Solicitacao?

  • Do you want to seek the "END" where? In the Solcaminho?

  • Friends, I’ve asked the question again...

  • Asked how the query was after the changes

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 1 more comment

4 answers

5

You did not specify which word you want to compare or which column to search for, adapt and test:

with t as (SELECT 'Inicio bla bla bla Meio bla Fim bla ww Fim 123 bla dihfidfh' as teste)

select SUBSTRING(teste, (len(teste) - CHARINDEX(REVERSE('Fim'), REVERSE(teste)) - 1), len(teste)) FROM t

Edited... again, I included a CASE:

select TOP 10 COUNT(SolID) as soma,
        CASE WHEN SolCaminho LIKE '% >> %'
                THEN SUBSTRING(SolCaminho, (len(SolCaminho) - CHARINDEX(REVERSE('>> '), REVERSE(SolCaminho)) + 2), len(SolCaminho))
                ELSE SolCaminho
           END as caminho
from Solicitacao where
DATEPART(m, SolData) = DATEPART(m, DATEADD(m, 0, getdate()))
AND DATEPART(yyyy, SolData) = DATEPART(yyyy, DATEADD(m, 0, getdate()))
and UsuIDGrupoRespConclusao = 2655
group by CASE WHEN SolCaminho LIKE '% >> %'
                THEN SUBSTRING(SolCaminho, (len(SolCaminho) - CHARINDEX(REVERSE('>> '), REVERSE(SolCaminho)) + 2), len(SolCaminho))
                ELSE SolCaminho
           END
order by soma desc

Note that I used the function on group by also, group after treating, if necessary use:

group by SolCaminho
  • Hey, buddy, I reread the question... I didn’t get it that way

  • Didn’t work out man, returned the whole names

1

I was researching about CHARINDEX on the Internet and ended up here, so I tried seek a solution to the problem since it has apparently not been solved.

REVERSE to invert the characters and CHARINDEX to pick the position of '>>'. With this I take the last '>>' of the sentence, right after the RIGHT function to return the right part of the sentence with the number of characters specified in CHARINDEX...

LTRIM() removes left spaces!

I’m not so good with explanations, anything, test EVERY FUNCTION of the example below and see how it works.

Example 1:

SELECT LTRIM(RIGHT('Faturamento >> Cupom Fiscal', CHARINDEX('>>', REVERSE('Faturamento >> Cupom Fiscal'))-1))

Example 2:

SELECT LTRIM(RIGHT('Faturamento >> Cupom Fiscal >> Farmácia Popular', CHARINDEX('>>', REVERSE('Faturamento >> Cupom Fiscal >> Farmácia Popular'))-1))
  • 1

    I believe your answer is the best in that question so far, so I would suggest that you add an explanation to the solution, making it even better

1

You can use a separation function and pick up the last part of the sequence:

SELECT (SELECT TOP(1) sep.item
          FROM separacao(s.solcaminho, ' >> ') sep
         ORDER BY sep.sequencia DESC) AS solcaminho
  FROM solicitacao s;

The code of the function used is as follows::

IF OBJECT_ID('separacao', 'TF') IS NULL
BEGIN
  EXEC('CREATE FUNCTION separacao() RETURNS @partes TABLE(t INT) AS BEGIN RETURN END');
END;
go

ALTER FUNCTION separacao(@frase       VARCHAR(MAX),
                         @delimitador VARCHAR(MAX) = ',')

RETURNS @partes TABLE (item      VARCHAR(MAX),
                       sequencia INTEGER)

BEGIN
  DECLARE @parte     VARCHAR(MAX)
  DECLARE @sequencia INTEGER

  SET @sequencia = 0;

  WHILE CHARINDEX(@delimitador, @frase, 0) <> 0
  BEGIN
    SET @parte = SUBSTRING(@frase, 1, CHARINDEX(@delimitador, @frase, 0) - 1);
    SET @frase = SUBSTRING(@frase, CHARINDEX(@delimitador, @frase, 0) + LEN(REPLACE(@delimitador, ' ', '_')), LEN(@frase));

    IF LEN(@parte) > 0
    BEGIN
      INSERT INTO @partes(item, sequencia)
      VALUES(@parte, @sequencia + 1);

      SET @sequencia = @sequencia + 1;
    END;
  END;

  IF LEN(@frase) > 0
  BEGIN
    INSERT INTO @partes(item, sequencia)
    VALUES(@frase, @sequencia + 1);
  END;

  RETURN;
END;
go

Here is the function code used and here you check the script to test the response.

0

Try it like this:

select substring('começo;meio#fim',
         charindex('#', 'começo;meio#fim') + 1, len('começo;meio#fim')
       )
  from tabela;

See working here on sqlfiddle.

  • If you have two identical words, in your case end, take from the first, this is not a problem ?

  • @Tiagooliveiradefreitas if it had two equal words, just delimit them with special characters. Which other way would be possible ?

  • Da to use the REVERSE in the column and in the word to compare, but in the case if he knows that only comes once the word or knows how many times this will come his answer is ideal, the code became cleaner.

  • @Nice... I’ll test your answer later, I didn’t know this method!

  • Friends, I repeated the question... I didn’t get it that way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.