How to bring only a certain part of a text in a field

4

How do I bring only part of the text in a field in the sql server?

I have the query below that in the Description column, it returns a text with lots of information, for example.

Nome Cliente    Data do ultimo Tramite     Descrição
Teste           25-07-2017                 de para teste ok  descricao do chamado

I want to bring the records that are in the description column, which may come from the word description. How do I do? Below is the query:

SELECT
    S.SolID,
    UC.UsuNome [Nome Cliente],
    MAX(T.TraData) [Data do Último Trâmite],
    MAX(CAST(T.Descricao as varchar(max))) [Descrição do Último Trâmite],
    U.UsuNome [Consultor Responsável],
    MAX(M.MotDesc) [Motivo da Pausa]
FROM
    Solicitacao S
    LEFT JOIN Usuario U ON U.UsuID = S.UsuIDResponsavel
    LEFT JOIN Usuario UC ON UC.UsuID = S.UsuIDCliente
    LEFT JOIN StatusMotivo SM ON SM.SMSolID = S.SolID
    INNER JOIN Motivo M ON M.MotID = SM.SMMotID
    INNER JOIN Tramite T on T.SolID = S.SolID and 
             T.TraID = (SELECT TOP 1 
                     X.TraID 
                 FROM
                     Tramite X 
                 WHERE
                    X.SolID = S.SolID
                ORDER BY
                    X.TraData DESC)
WHERE
    S.VencimentoPausado = 1
    AND s.usuidresponsavel = 91258
    AND cast(T.TraData as date) <= cast(DATEADD(day,-2,getdate()) as date)
GROUP BY
    S.SolID,
    UC.UsuNome,
    U.UsuNome
    
asked by anonymous 27.07.2017 / 13:48

1 answer

0

Based on the response of the comment, follow the code snippet. But it will always catch the first occurrence of the term blzz;) Test to see if it is OK. as I do not have a bench to test it needs validation.

SELECT
    S.SolID,
    UC.UsuNome [Nome Cliente],
    MAX(T.TraData) [Data do Último Trâmite],
    MAX(CAST(T.Descricao as varchar(max))) [Descrição do Último Trâmite],
    U.UsuNome [Consultor Responsável],
    MAX(M.MotDesc) [Motivo da Pausa],
    MAX(SUBSTRING(T.Descricao, PATINDEX('%termoPesquisa%', T.Descricao), LEN(T.Descricao))) [Termo Filtrado]
FROM
    Solicitacao S
    LEFT JOIN Usuario U ON U.UsuID = S.UsuIDResponsavel
    LEFT JOIN Usuario UC ON UC.UsuID = S.UsuIDCliente
    LEFT JOIN StatusMotivo SM ON SM.SMSolID = S.SolID
    INNER JOIN Motivo M ON M.MotID = SM.SMMotID
    INNER JOIN Tramite T on T.SolID = S.SolID and 
             T.TraID = (SELECT TOP 1 
                     X.TraID 
                 FROM
                     Tramite X 
                 WHERE
                    X.SolID = S.SolID
                ORDER BY
                    X.TraData DESC)
WHERE
    S.VencimentoPausado = 1
    AND s.usuidresponsavel = 91258
    AND cast(T.TraData as date) <= cast(DATEADD(day,-2,getdate()) as date)
GROUP BY
    S.SolID,
    UC.UsuNome,
    U.UsuNome
    
27.07.2017 / 13:56