1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| -- ==========
-- Paramètres
-- ==========
SET NOCOUNT ON
SET DATEFORMAT ymd
-- ===========================
-- Suppression Database 'base'
-- ===========================
if db_id(N'base') is not null
drop database base
-- ========================
-- Création Database 'base'
-- ========================
create database base
collate French_CI_AS
WITH TRUSTWORTHY ON,
DB_CHAINING OFF
use base
Le contexte de la base de données a changé*; il est maintenant 'base'.
-- =====================
-- Création Table 'test'
-- =====================
create table test (
id smallint identity(1, 1) NOT NULL,
date_deb datetime NOT NULL,
date_fin datetime NOT NULL,
constraint pk_test_id primary key clustered (id)
)
-- =====================
-- Insertion dans 'test'
-- =====================
INSERT INTO test (date_deb,date_fin) VALUES
('2016-05-08 15:00:00', '2016-05-08 15:21:11'),
('2016-05-08 15:00:00', '2016-05-10 15:21:11')
-- ================
-- Vidage de 'test'
-- ================
select * from test
id date_deb date_fin
------ ----------------------- -----------------------
1 2016-05-08 15:00:00.000 2016-05-08 15:21:11.000
2 2016-05-08 15:00:00.000 2016-05-10 15:21:11.000
-- ===========
-- Requête CTE
-- ===========
with CTE (id, diff) as (
select id, datediff(second, date_deb, date_fin) as diff
from test
)
select id, diff, concat(diff/86400, ' jours ', convert(varchar, dateadd(second, diff%86400, '00:00:00'), 108)) as affichage
from CTE
id diff affichage
------ ----------- -------------------------------------------------
1 1271 0 jours 00:21:11
2 174071 2 jours 00:21:11
Appuyez sur une touche pour continuer... |
Partager