How to make multiple mask Replaces per table field with T-SQL

Asked

Viewed 252 times

1

I am performing some texts (Mask) Replaces from a field of a temporary table. I am substituting where there is the text searched for a field of other tables.

Example:

SELECT TOP 1000 * INTO #TEMP_COMPL_L FROM TB_CONTRATO WHERE IDCAR_CON = 1
ALTER TABLE #TEMP_COMPL_L ADD COMPL_TMP VARCHAR(1500)

UPDATE #TEMP_COMPL_L set COMPL_TMP = '#PRINOME#, URGENTE: Va a sua loja no dia #DATA# e procure o Gerente #NOMEGERENTE#.'

-- INSERIR DADOS DAS MÁSCARAS AQUI
UPDATE #TEMP_COMPL_L SET COMPL_TMP = 
REPLACE(
REPLACE(
REPLACE(COMPL_TMP,
    '#PRINOME#',LEFT(NOME_CLI, CHARINDEX(' ',NOME_CLI)-1))
    ,'#NOMEGERENTE#',ISNULL(CONTT_LOJ,''))
    ,'#DATA#',CONVERT(VARCHAR(10),GETDATE(),103)
    )
FROM #TEMP_COMPL_L INNER JOIN TB_CLIENTE ON IDCLI_CON = IDCLI_CLI
LEFT JOIN TB_LOJA ON IDLOJ_CON = IDLOJ_LOJ

This way it works perfectly, however, I will need to do this for more than 20 possible masks informed by the customer, picking which is the mask and giving a replace to the value of the corresponding field of the table STORE and CUSTOMER.

There is a way to leave it with a better and noticeably readable performance, as several Places will get very confusing, plus have a limit of Places at once.

No answers

Browser other questions tagged

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