Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Intervalo entre datas


Fernandinha

Question

Bom dia pessoal,

Estou com problemas com datas, vocês poderiam me dar um help por favor?

Tenho duas tabelas, na primeira tabela tenho as seguintes informações:

Tabela A

DtInicTurma DtFinalTurma

2011-08-15 00:00:00.000 2011-08-24 00:00:00.000

Na outra tabela tenho as datas:

Tabela B

DtRegistro

2011-08-15 00:00:00.000

2011-08-16 00:00:00.000

2011-08-17 00:00:00.000

2011-08-22 00:00:00.000

2011-08-23 00:00:00.000

2011-08-24 00:00:00.000

Preciso saber quais datas não tenho na tabela B de acordo com o range passado na tabela A.

Desde já agradeço a ajuda.

Edited by Fernandinha
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Bom dia Fernandinha,

Fiz um exemplo comentado com duas temporárias. Espero que ajude.

-- Criação das temporárias
create table #TabelaA (DtInicTurma datetime, DtFinalTurma DATETIME)
create table #TabelaB (DtRegistro DATETIME)

-- Inserção dos dados
INSERT INTO #TabelaA VALUES ('2011-08-15 00:00:00.000', '2011-08-24 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-15 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-16 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-17 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-22 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-23 00:00:00.000')
INSERT INTO #TabelaB VALUES ('2011-08-24 00:00:00.000')

-- Selecionar e executar daqui até o final
DECLARE @DataInicial DATETIME
DECLARE @DataFinal DATETIME

SET @DataInicial = (SELECT DtInicTurma from #TabelaA) 
SET @DataFinal =  (SELECT DtFinalTurma from #TabelaA)

WHILE @DataInicial <= @DataFinal
BEGIN
    IF (SELECT top 1 1 FROM #TabelaB WHERE DtRegistro=@DataInicial) IS NULL 
    BEGIN
        PRINT convert(varchar(10),@DataInicial, 103)    
    END
    SET @DataInicial = DATEADD (d, 1, @DataInicial)
END

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...