I
I
impressive172019-12-22 16:17:44
SQL
impressive17, 2019-12-22 16:17:44

How to make the date displayed by the number of hours in the database?

I have a field that displays a time difference and it splits the time into hours, minutes, seconds. I want to display this field so that it only shows the difference in hours with a single float value. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexV, 2019-12-23
@Av-IT

-- Let @t_start be a start date in the past, for example 1111 minutes ago
DECLARE @t_start AS DATETIME = DATEADD(minute, -1111, GETDATE())
-- Find out how many hours have passed since that time as a FLOAT using
SELECT DATEDIFF(second , @t_start, getdate()) / 3600.0

S
Sergey c0re, 2019-12-24
@erge

assuming that the variable is of type DATETIME, then something like this:

-- дата начала
DECLARE @t_start AS DATETIME = DATEADD(minute, -7410, GETDATE());
-- дата конца
DECLARE @t_finish AS DATETIME = GETDATE();
-- разница между датами начала и конца в переменной типа DATETIME
DECLARE @t_diff AS DATETIME = @t_finish - @t_start;

-- разница между датами начала и конца
SELECT @t_diff;

SELECT DATEDIFF(second, DATEFROMPARTS(1900,1,1), @t_diff) / 3600.0 AS diff_h

see https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=9...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question