Query to get text after a certain character

5

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

Ineedtoalwayssearchfortheremainderofthestringafterthelast"" " ie for the first line I needed to get all " System access " / strong>

In the second row I need to get ONLY "People's Pharmacy" because 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. I already posted this question here for some time but I did not get results.

Thank you.

    
asked by anonymous 11.11.2015 / 15:04

2 answers

1

You can get the position of the last occurrence of >> using the LOCATE function in conjunction with REVERSE and then use the amount of characters found in this result to return the characters to the right, using RIGHT :

SELECT CASE x.posicao
         WHEN 0 THEN x.solcaminho
         else RIGHT(x.solcaminho, x.posicao - 1)
       END AS solcaminho
  FROM (SELECT *,
               LOCATE(' >> ', REVERSE(s.solcaminho)) AS posicao
          FROM solicitacao s) x

Here you check the SQL Fiddler with the proposal working .

    
27.08.2017 / 14:38
-1

After the query you can do this, before returning the query result to the grid or another element you are using, just pick up the code and play inside a PHP file and take the test.

// aqui é o campo caminho que vai retornar da consulta.

$caminho ="Faturamento>>Cupom>>Add";
echo "Caminho a ser quebrado<br>".$caminho;
$string = explode('>>', $caminho);

echo "<br>String quebrada<br>";
echo "<pre>";// echo pre

print_r($string);
$s = (count($string))==1?'0':count($string)-1;

echo "vou imprimir so a ultima parte do caminho ".$string[$s];
    
11.11.2015 / 16:16