0
Come on, guys, I’m putting together an SQL query. In this query, the field SD1010.D1_TES can assume N values, but if this field is equal to empty, I need one more restriction to be applied (SF4010.F4_TEXTO NOT LIKE '%*%'). For this, first tested use conditionals with CASE and also with if, follows example below:
WHERE
.
.
. AND
(
CASE
WHEN SD1010.D1_TES = ' ' THEN (SF4010.F4_TEXTO NOT LIKE '%*%')
)
It turns out that SQL points error, already tried to put parentheses, take parentheses, use if Lse and even then the error happens.
The only way I could was to duplicate the restrictions using OR and only add the restriction of D1_TES and F4_TEXTO:
WHERE
(
**SD1010.D1_TES = ' ' AND**
-- RETIRA DELETADOS
(SD1010.D_E_L_E_T_<>'*') AND
(
--CLASSIFICADO E ATUALIZOU ESTOQUE
SF4010.F4_ESTOQUE = 'S' OR
--PENDENTE DE CLASSIFICACAO PARA ESTOQUE
(SD1010.D1_CC='' AND SD1010.D1_TES='')
) AND
SF1010.F1_DTDIGIT>=concat(YEAR(GETDATE()),'0',month(GETDATE())-1,'01') AND -- DADOS INSERIDOS A PARTIR DO PRIMEIRO DIA DO MÊS ANTERIOR
SF1010.F1_DTDIGIT<=GETDATE() AND -- DADOS INSERIDOS ATÉ O DIA DE HOJE
(SF4010.F4_TEXTO NOT LIKE '%*%') AND -- retira importação 1
(SF1010.F1_ESPECIE <> 'SPE ' OR SF1010.F1_ESPECIE <> 'SPE') AND -- retira importação 2
(SF1010.F1_ESPECIE NOT LIKE 'NFE') AND -- retira importação 3
(SD1010.D1_FILIAL = '09')-- FILTRA FILIAL
) OR
(
-- RETIRA DELETADOS
(SD1010.D_E_L_E_T_<>'*') AND
(
--CLASSIFICADO E ATUALIZOU ESTOQUE
SF4010.F4_ESTOQUE = 'S' OR
--PENDENTE DE CLASSIFICACAO PARA ESTOQUE
(SD1010.D1_CC='' AND SD1010.D1_TES='')
) AND
SF1010.F1_DTDIGIT>=concat(YEAR(GETDATE()),'0',month(GETDATE())-1,'01') AND -- DADOS INSERIDOS A PARTIR DO PRIMEIRO DIA DO MÊS ANTERIOR
SF1010.F1_DTDIGIT<=GETDATE() AND -- DADOS INSERIDOS ATÉ O DIA DE HOJE
(SF1010.F1_ESPECIE <> 'SPE ' OR SF1010.F1_ESPECIE <> 'SPE') AND -- retira importação 2
(SF1010.F1_ESPECIE NOT LIKE 'NFE') AND -- retira importação 3
(SD1010.D1_FILIAL = '09')-- FILTRA FILIAL
)
Does anyone know any way that I can make a condition for restricting or wiping this code and not have to repeat these restrictions?
is using
sql-server
right? add this tag to facilitate understanding– Ricardo Pontual
What is the purpose of this filter:
(SF1010.F1_ESPECIE <> 'SPE ' OR SF1010.F1_ESPECIE <> 'SPE')
? It seems to me that he always returns "true".– José Diz