Select com intervalo de horas
24/04/2017
0
Estou precisando da seguinte ajuda.
Trabalho com VS 2015 e SQLServer 2014.
Preciso fazer uma agenda médica, e tenho uma tabela com a seguinte estrutura:
tempo de atendimento Início Fim Intervalo
15 minutos 08:00 18:00 12:00 até 13:00
Preciso fazer um select que retorne o seguinte dado:
08:00
08:15
08:30
08:45
09:00
etc..
Até o fim que no caso é 18:00, respeitando o intervalo que no caso é de 12:00 até as 13:00
Muito obrigado
Márcio.
Deus abençoe.
Márcio Oliveira
Post mais votado
24/04/2017
abraços
DECLARE @TEMPO TIME = '08:00'
DECLARE @TABELA TABLE(TEMPO VARCHAR(5))
WHILE(@TEMPO <= '18:00')
BEGIN
if(@TEMPO not between '12:00' and '12:45')
begin
INSERT INTO @TABELA
SELECT @TEMPO
end
SET @TEMPO = DATEADD(MINUTE,15,@TEMPO)
END
SELECT * FROM @TABELA
Lincon Silva
Mais Posts
24/04/2017
Lincon Silva
Nesse caso eu indico o uso de um cursor. é a unica coisa que me vem na mente agora.
Minha pergunta é: Você precisa adicionar em uma tabela existente ou é apenas pra carregar algum objeto com esses dados (apenas com as informações de horário) ?
24/04/2017
Luiz Santos
Só altere if(@TEMPO not between '12:00' and '12:45') por if(@TEMPO not between '12:01' and '12:45').
Assim o valor 12:00 aparece na consulta.
Grande abraço
24/04/2017
Lincon Silva
Só altere if(@TEMPO not between '12:00' and '12:45') por if(@TEMPO not between '12:01' and '12:45').
Assim o valor 12:00 aparece na consulta.
Grande abraço
Show de bola mestre.
Abraços,
25/04/2017
Márcio Oliveira
Deus abençoe a cada um que se dispôs a me ajudar. A solução funcionou maravilhosamente bem. Adaptei e fiz dessa forma:
USE [SGI]
GO
/****** Object: StoredProcedure [dbo].[prc_Horarios] Script Date: 25/04/2017 10:22:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[prc_Horarios](@TempoINI TIME,
@TempoFIM TIME,
@IntervaloINI TIME,
@IntervaloFIM TIME,
@TempoATENDIMENTO int)
AS
begin
DECLARE @TABELA TABLE(Horários VARCHAR(5));
Begin
While(@TempoINI <= @TempoFIM)
Begin
if(@TempoINI not between @IntervaloINI and @IntervaloFIM)
begin
Insert Into @TABELA
Select @TempoINI
end
Set @TempoINI = DATEADD(MINUTE,@TempoATENDIMENTO,@TempoINI)
end
end;
Select * FROM @TABELA
end
OBS: Post resolvido.
25/04/2017
Lincon Silva
Deus abençoe a cada um que se dispôs a me ajudar. A solução funcionou maravilhosamente bem. Adaptei e fiz dessa forma:
USE [SGI]
GO
/****** Object: StoredProcedure [dbo].[prc_Horarios] Script Date: 25/04/2017 10:22:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[prc_Horarios](@TempoINI TIME,
@TempoFIM TIME,
@IntervaloINI TIME,
@IntervaloFIM TIME,
@TempoATENDIMENTO int)
AS
begin
DECLARE @TABELA TABLE(Horários VARCHAR(5));
Begin
While(@TempoINI <= @TempoFIM)
Begin
if(@TempoINI not between @IntervaloINI and @IntervaloFIM)
begin
Insert Into @TABELA
Select @TempoINI
end
Set @TempoINI = DATEADD(MINUTE,@TempoATENDIMENTO,@TempoINI)
end
end;
Select * FROM @TABELA
end
OBS: Post resolvido.
Muito bom Márcio. Abraços,
Clique aqui para fazer login e interagir na Comunidade :)