Query to get a word after a certain character

1

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

Ineedtoalwaysfetchtheremainderofthestringafterthelast"" ", ie for the first line I needed to get all " System access " "" "

In the second row, I need to get "People's Pharmacy" as it is after the LAST

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 SUBSTRING with CharIndex but I did not succeed.

    
asked by anonymous 09.11.2015 / 14:24

4 answers

5

You did not enter which word to compare or which column to fetch, 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 in group by also, group after processing, if necessary use:

group by SolCaminho
    
09.11.2015 / 18:42
1

I was searching on CHARINDEX over the internet and I ended up here, so I tried seek a solution to the problem since it has apparently not been resolved.

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

LTRIM () removes spaces on the left!

I'm not so good with explanations, anything, test EVERY FUNCTION from 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))
    
18.08.2017 / 19:42
1

You can use a separator function and take 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 function code used is the following:

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 and here you give script to test the answer.

    
18.08.2017 / 20:06
0

Try this:

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

See working here in sqlfiddle .

    
09.11.2015 / 18:35