I want to convert 300 minutes into 5 hours.
Input is 300
Output should be 05:00
Try this:
select convert(varchar(5), dateadd(MINUTE, 300, '1970-01-01'), 108)
Try this
`
DECLARE
#FirstDate datetime,
#LastDate datetime
SELECT
#FirstDate = '2000-01-01 09:00:00',
#LastDate = '2000-01-01 11:30:00'
SELECT CONVERT(varchar(12),
DATEADD(minute, DATEDIFF(minute, #FirstDate, #LastDate), 0), 114)
`
Related
I would like into a stored procedure, truncate timestamp input values at the top hour or at the lower hour.
For example, if my input values are 2020-02-12 06:56:00 and 2020-02-12 07:14:00, I would like to transforme it in 2020-02-12 06:00:00 and 2020-02-12 08:00:00
Is a cast function can work?
You can construct the new datetimes from the parts that you want of your original datetimes.
declare #start datetime = '2020-02-12 06:56:00'
declare #end datetime = '2020-02-12 07:14:00'
select #start as OriginalStart,
#end as OriginalEnd,
datetimefromparts(year(#start), month(#start), day(#start), datepart(hour, #start), 0, 0, 0) as TruncatedStart,
dateadd(hour, 1, datetimefromparts(year(#end), month(#end), day(#end), datepart(hour, #end), 0, 0, 0)) as TruncatedEnd
The first truncation of the interval is the lower hour, and the second one adds an additional hour so it returns the higher hour.
PS: If what you want is to round to the nearest hour, then you can add 30 minutes and truncate :
declare #date datetime = '2020-02-12 06:56:00'
set #date = dateadd(minute, 30, #date)
select datetimefromparts(year(#date), month(#date), day(#date), datepart(hour, #date), 0, 0, 0) as NearestHour
or in a single step (using Lepetit's shortcut for truncation) :
declare #date datetime = '2020-02-12 06:56:00'
select dateadd(hour, datediff(hour, 0, dateadd(minute, 30, #date)), 0) AS NearestHour
This is a simpler solution:
declare #start datetime = '2020-02-12 06:56:00'
declare #end datetime = '2020-02-12 07:14:00'
select #start as OriginalStart,
#end as OriginalEnd,
dateadd(hour, datediff(hour, 0, #start), 0) as TruncatedStart,
dateadd(hour, datediff(hour, 0, dateadd(hour, 1, #end)), 0) as TruncatedEnd
In both cases the function substracts the hour part from the original timestamp. For the TruncatedEnd, one hour is added, so that the result is the subsequent hour.
Using a bit of arithmetic calculation, convert to hours with decimal and use floor() and ceiling() to perform the round up / down
first it find the time different with 00:00:00 in terms of second. convert(date, date_col) convert the datetime to date, so effectively it is 00:00:00
datediff(second, convert(date, date_col), date_col)
then you divide by 60 x 60 = 3600 seconds. Gives you fraction of hours
then you use floor() or ceiling() to perform the rounding
and lastly you add that back to the date (convert(date, date_col))
Final query
select *,
RoundDown = convert(datetime, convert(date, date_col))
+ dateadd(hour, floor(datediff(second, convert(date, date_col), date_col) / (3600.0)), 0),
RoundUp = convert(datetime, convert(date, date_col))
+ dateadd(hour, ceiling(datediff(second, convert(date, date_col), date_col) / (3600.0)), 0)
from (
values
('2020-02-12 06:56:00'),
('2020-02-12 07:14:00')
) d (date_col)
/*
2020-02-12 06:56:00 2020-02-12 06:00:00 2020-02-12 07:00:00
2020-02-12 07:14:00 2020-02-12 07:00:00 2020-02-12 08:00:00
*/
EDIT : a much simpler query below
find the different in minute divide by 60.0 minutes to get different in terms of hour (with decimal places) and then apply floor or ceiling. Finally add that result back
select getdate() as Now,
dateadd(hour, floor(datediff(minute, 0, getdate()) / 60.0), 0) as RoundDown,
dateadd(hour, ceiling(datediff(minute, 0, getdate()) / 60.0), 0) as RoundUp
I am trying to convert week of the date based on my criteria.
My date condition: if my #date is less than 4 AM, then #date - 1, else #date
declare #dates datetime
set #dates = '2019-01-01 03:59:59'
select
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then convert(varchar, dateadd(day, -1, #dates), 103)
else convert(varchar, #dates, 103)
end BusinessDate
Output:
31/12/2018 // as expected
Now I want to find the week number of the output. So I tried
declare #dates datetime
set #dates = '2019-01-01 03:59:59'
select
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then convert(varchar, dateadd(day, -1, #dates), 103)
else convert(varchar, #dates, 103)
end BusinessDate,
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then datepart(week, convert(datetime, convert(varchar, dateadd(day, -1, #dates), 103)))
else datepart(week, convert(datetime, convert(varchar, #dates, 103)))
end weeks
But I get this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Just subtract four hours:
select datepart(week,
dateadd(hour, -4, #dates)
)
I have a table with timestamp that I want to round off at 15 min. interval. I can round off using the below Query but it rounds off both 11:58 and 12:02 to 12:00 which is not what I want. I would like to round off timestamp at 15 min. interval which gives me time_untill ie for anything between 11:45 to 11:59 should be rounded off to 12 and anything between 12:00 to 12:14 should be rounded off to 12:15. Please let me know how can I achieve that? Thanks
SELECT transaction_id,
CONVERT(smalldatetime, ROUND(CONVERT(float, CONVERT(datetime, entry_date_time)) * 96.0, 0, 1) /96.0) as transaction_datetime
FROM <table>
You can use datetimefromparts():
select dateadd(minute,
15,
datetimefromparts(year(entry_date_time), month(entry_date_time), day(entry_date_time),
datepart(hour, entry_date_time),
15 * (datepart(minute, entry_date_time) / 15), 0, 0
)
) as roundup15
You could use the DATEADD/DATEDIFF method to truncate date/time values that's been available for a long time.
SELECT transaction_id,
entry_date_time,
DATEADD( MI, DATEDIFF( MI, '2010', entry_date_time)/15*15, '2010') as transaction_datetime
--FROM Sample Data
FROM (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) transaction_id,
DATEADD( SS, CHECKSUM(NEWID())%10000, CAST( GETDATE() AS smalldatetime)) AS entry_date_time
FROM sys.columns)x;
Something like this...
DECLARE #time TIME(0) = GETDATE();
SELECT
DATEADD(MINUTE,
(((DATEDIFF(MINUTE, '00:00:00', #time) % 60) / 15) * 15),
DATEADD(HOUR, DATEDIFF(HOUR, '00:00:00', #time), '00:00:00')
);
I have a table where start and end date is available which are in datetime format.
I have two requirements.
Get the difference in hh:mm:ss format.
Get the AVG of that time.
I am able to achieve first requirement but not sure how to get AVG after that. Do I need to cast on the statement where I am getting hh:mm:ss?
Query:
DECLARE #StartDate DATETIME, #EndDate DATETIME, #StartDate2 DATETIME, #EndDate2 DATETIME;
SET #StartDate = '2018-07-17 10:56:06.003';
SET #EndDate = '2018-07-17 10:57:15.967';
SET #StartDate2 = '2018-06-06 15:57:47.823';
SET #EndDate2 = '2018-06-06 16:01:28.707';
SELECT #StartDate AS StartDate,
#EndDate AS EndDate,
CONVERT(VARCHAR(5), DATEDIFF(s, #StartDate, #EndDate) / 3600)+':'+CONVERT(VARCHAR(5), DATEDIFF(s, #StartDate, #EndDate) % 3600 / 60)+':'+CONVERT(VARCHAR(5), (DATEDIFF(s, #StartDate, #EndDate) % 60)) AS [hh:mm:ss],
0 as placeholder
UNION
SELECT #StartDate2 AS StartDate2,
#EndDate2 AS EndDate2,
CONVERT(VARCHAR(5), DATEDIFF(s, #StartDate2, #EndDate2) / 3600)+':'+CONVERT(VARCHAR(5), DATEDIFF(s, #StartDate2, #EndDate2) % 3600 / 60)+':'+CONVERT(VARCHAR(5), (DATEDIFF(s, #StartDate2, #EndDate2) % 60)) AS [hh:mm:ss],
0 as placeholder;
Result:
StartDate EndDate hh:mm:ss placeholder
----------------------- ----------------------- ----------------- -----------
2018-06-06 15:57:47.823 2018-06-06 16:01:28.707 0:3:41 0
2018-07-17 10:56:06.003 2018-07-17 10:57:15.967 0:1:9 0
Here, my expected result in placeholder would be average of 3:41 and 1:9.
If I have two datetimes like this :
transtime_in, transtime_out
How to get the difference between those datetimes in the following format :
hh:mm
I use
DATEDIFF(hour, transtime_in, transtime_out)
but i get the hours only .
Try this one -
Query:
DECLARE
#transtime_in DATETIME
, #transtime_out DATETIME
SELECT
#transtime_in = '2013-05-19 08:58:07.000'
, #transtime_out = '2013-05-19 16:40:53.000'
SELECT LEFT(CONVERT(VARCHAR(10), #transtime_out - #transtime_in, 108), 5)
Output:
-----
07:42
declare #D1 datetime
declare #D2 datetime
set #D1 = '2014-03-25 00:00:00.000'
set #D2 = '2014-03-24 17:14:05.000'
--select datediff(hour, cast(#D1 as time(0)), cast(#D2 as time(0)))
SELECT LEFT(CONVERT(VARCHAR(10), #D2 - #D1, 108), 8)
declare #D1 datetime
declare #D2 datetime
set #D1 = '2014-03-25 00:00:00.000'
set #D2 = '2014-03-24 17:14:05.000'
SELECT LEFT(CONVERT(VARCHAR(10), #D1 - #D2, 108), 8)