Date choices

Asked

Viewed 33 times

-1

Good afternoon, you guys,

I’m looking to make a iif in sql that if the field FLAG is equal to "And" it brings the date of the column DTDIGIT and store in variable @dataInicio, and when the FLAG for "S" it brings the date of the column EMISSAO and store in variable @dataFIM.

And then I declare these variables, because that _ query_ will become a function.

Can someone help me?

  • The FLAG field is stored where? What is a field in this context? Note that in the statement both "field" and "column" are cited. // How is the source table? // How are DTDIGIT and EMISSAL columns declared?

  • Good morning the flag field is a column that the only result of it is E or S (input or output) the emitted column and dtdigit are of type date,

  • It would be something +/-like IIF (FLAG = 'E', DTDIGIT OR FLAG = ’S'OUTPUT)

1 answer

0

If there is a single row with the FLAG column containing "S" and another row containing the value "E", both meeting the filter defined in the WHERE clause, the code below returns what is requested.

-- código #1
SELECT @dataInicio= case when FLAG = 'E' then DTDIGIT else @dataInicio end,
       @dataFim= case when FLAG = 'S' then EMISSAO else @dataFim end
  from tabela
  where ...;

Example:

-- código #2
CREATE TABLE Config (FLAG char(1), DTDIGIT date, EMISSAO date);

INSERT into Config values
  ('E', '20120303', NULL), ('S', NULL, '20120403');

declare @dataInicio date, @dataFim date;

SELECT @dataInicio= case when FLAG = 'E' then DTDIGIT else @dataInicio end,
       @dataFim= case when FLAG = 'S' then EMISSAO else @dataFim end
  from Config;

SELECT @dataInicio, @dataFim; 

However, if there is more than one row with the same value for the FLAG column, the completion of variables is undetermined.

  • Thanks for the help, but I think your case is not filtering tried to do something +/- like WHERE IIF(FLAG = 'E', DTDIGIT, EMISSAO) < '20180101' But now I doubt how to put in the variables for the person to choose from when to when to want the information regarding the dates

  • It will be useful if, in the topic statement, you add sample of the data as well as expected response to the sample.

  • I’ll send you the query

  • SELECT FN.COMPANY, FN. AFFILIATE, FN.DOC NOTA_FISCAL, ISNULL (A2.A2_COD,A1_COD) [COD VENDOR], ISNULL (A2.A2_NOME,A1_NOME) [ SUPPLIER NAME], FN.TYPE, FN.NFORI NOTA_DE_ORIGEM, FN.CUSTO1 COST, FNI.NUMSERI, P.COD COD_PRODUTO, P.DESCR DESCRICAO_PRODUTO, O.OPERATION, O.DESCRI DESCRICAO_OOPERACAO, CAST(FN.EMITTED AS datetime)DATA, FN.FLAG --IIF (FLAG = 'E', DTDIGIT FLAG = ’S', ISSUANCE) FROM TLX_NFI_ENTRADASAIDA FN WHERE IIF(FLAG = 'E', DTDIGIT, ISSUANCE) < '20180101'

  • In SQL Server it is not possible, in the same SELECT statement, to return results and at the same time to mark values for variables.

Browser other questions tagged

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