-
Notifications
You must be signed in to change notification settings - Fork 3
/
04_Tempo_Entre_DatasHoras.sql
19 lines (16 loc) · 885 Bytes
/
04_Tempo_Entre_DatasHoras.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- Defina suas duas datas
DECLARE @dataInicial DATETIME = '2022-01-01';
DECLARE @dataFinal DATETIME = '2023-01-01';
-- Calcule a diferença em dias
DECLARE @diasPassados INT;
SET @diasPassados = DATEDIFF(DAY, @dataInicial, @dataFinal);
-- Calcule a diferença em horas, minutos e segundos
DECLARE @horasPassadas INT, @minutosPassados INT, @segundosPassados INT;
SET @horasPassadas = DATEDIFF(HOUR, @dataInicial, @dataFinal);
SET @minutosPassados = DATEDIFF(MINUTE, @dataInicial, @dataFinal);
SET @segundosPassados = DATEDIFF(SECOND, @dataInicial, @dataFinal);
-- Exiba os resultados
PRINT 'Passaram-se ' + CAST(@diasPassados AS VARCHAR) + ' dias.';
PRINT 'Passaram-se ' + CAST(@horasPassadas AS VARCHAR) + ' horas.';
PRINT 'Passaram-se ' + CAST(@minutosPassados AS VARCHAR) + ' minutos.';
PRINT 'Passaram-se ' + CAST(@segundosPassados AS VARCHAR) + ' segundos.';