I have a column called total_hours which is a set of times in Time type and it is in HH:MM:SS format in SQL server. I want to know how can I get the sum of total_hours.
For example:
total_hours
00:10:30
01:20:00
00:20:00
---------
01:50:30
Could you please give me the query to exactly do the same thing with the same format?
Try this
SELECT SUM( DATEPART(SECOND, [time]) + 60 *
DATEPART(MINUTE, [time]) + 3600 *
DATEPART(HOUR, [time] )
) as 'TotalTime'
FROM Table1
(Or)
SELECT dateadd(millisecond,sum(datediff(millisecond,0, [Time])),0)
FROM Table1
Related
I'm trying to SELECT all the rows from a SQL database which are between an hour interval, for every day.
The datetime column is called "Dt" and has the following datetime format: 2019-10-17 16:03:43
I'd like to extract all the rows from this table where the Dt was between 22:00:00 and 02:00:00, for everyday.
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN '*-*- 22:00:00' AND '*-*- 02:00:00';
where * should be any...
Thanks for your support!
EDIT: I forgot to mention: I'm using the integrated SQL interpreter from DB Browser for SQLite
You need to extract the time part of the date and compare that it is within the range. Since midnight is between 22 and 2, you will need to split it to two comparisons, time between 22 and 0 and between 0 and 2.
To see how to extract the time take a look at this question.
With Postgres, assuming dt is defined as timestamp you can do the following:
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN "Dt"::date + time '22:00:00' and ("Dt"::date + 1) + time '02:00:00'
Or if you want to exclude timestamps at precisely 02:00:00
SELECT *
FROM MY_TABLE
WHERE "Dt" >= "Dt"::date + time '22:00:00'
and "Dt" < ("Dt"::date + 1) + time '02:00:00'
select DT_time from (
select cast (substr(to_char(Dt,'dd-mm-yyyy HH:MM:SS'),12,2) as integer ) as DT_time from MY_TABLE )
where DT_time between 2 and 22;
between 22:00:00 and 02:00:00
means:
SELECT *
FROM MY_TABLE
WHERE
substr(Dt, 12) BETWEEN '22:00:00' AND '23:59:59'
OR
substr(Dt, 12) BETWEEN '00:00:00' AND '02:00:00'
This will work ::
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
AND DATEPART(HOUR, Dt)<2
Update :
SELECT *
FROM MY_TABLE
WHERE Dt Between DATEADD (hour,22,DATEADD(day, DATEDIFF(day, 0, Dt), 0)) AND DATEADD (hour,2,DATEADD(day, DATEDIFF(day, -1, Dt), 0))
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
OR DATEPART(HOUR, Dt)<2
Above query work for you..
1st one will check only for particular date and consecutive next date along with your time range.
But If you don't care about dates and only looking for time interval in particular hours then 2nd one is for you.
For SQLite :
SELECT *
FROM MY_TABLE
WHERE strftime('%H','Dt')>22
OR strftime('%H','Dt')<2
how to get hour by hour data (no matter what is the date) in sql server, I know it can be achieved with datePart(hour, columnname) but to be specific, I need data for specific intervals including minutes regardless of dates.
Scenario: 'TestTable' contains column - DBTimestamp with data type (DateTime)
I need all Records from 'TestTable' for which 'DBTimestamp' is between 3 Hours 35 Minutes And 4 Hours 30 Minutes regardless of date specified.
You can use this.
SELECT * FROM TestTable WHERE CAST(DBTimestamp AS TIME) BETWEEN '03:35' AND '04:30'
From my understanding you need records that in specific range of time regardless of date part. So I've come up with following solution:
SELECT * FROM (SELECT Id, CAST(Time AS time) [time]
FROM Table) AS Q1
WHERE Q1.time > CAST('03:35:00' AS time)
AND Q1.time < CAST('12:30:00' AS time)
Here is SQL Fiddle:
http://sqlfiddle.com/#!6/21b313/1
Well I guess you could do some variation of the following:
SELECT *
FROM TABLE
WHERE (DATEPART(HOUR, TIMESTAMP) = 3 AND DATEPART(MINUTE, TIMESTAMP) >= 35) OR
(DATEPART(HOUR, TIMESTAMP) = 4 AND DATEPART(MINUTE, TIMESTAMP) <= 30)
I am trying to sum the hours from a datetime column in a sql but I am getting an error when I try this:
SELECT
DATEADD(HH, SUM(DATEDIFF(HH, '00:00:00.000', myDate)), '00:00:00.000') as Total_Hours
FROM myTable
WHERE ID in (1, 2)
My date column looks like this
2015-02-18 00:00:00.000
2015-02-18 00:30:00.000
2015-02-18 01:00:00.000
2015-02-18 01:30:00.000
How can I sum and only show the total hours with out the date?
Perhaps you want to extract the hours and add them up? The conversion back to a data is definitely unnecessary:
SELECT SUM(DATEPART(hour, myDate)) as Total_Hours
FROM myTable
WHERE ID in (1, 2);
EDIT;
If you want to add the time component, then use minutes or seconds:
SELECT SUM(DATEPART(second, myDate)) / (60.0 * 60.0) as Total_Hours
FROM myTable
WHERE ID in (1, 2);
I get the impression that you want to add the time parts together:
select Total_Hours = datepart(hour, cast(myDate as time))
from myTable
where id in (1, 2)
I have query which gives sum of total talk time for an Agent all the years
SELECT
CONVERT(TIME, DATEADD(s, SUM(( DATEPART(hh, TalkTime) * 3600 ) + ( DATEPART(mi, TalkTime) * 60 ) + DATEPART(ss, TalkTime)), 0))
FROM [CRMIntegration].[dbo].[Five9CallLog]
where AGENT_NAME = 'XYZ'
which is giving me '00:37:01.0000000'
but when i sum up total talk time for this year till now using the below query
SELECT
CONVERT(TIME, DATEADD(s, SUM(( DATEPART(hh, TalkTime) * 3600 ) + ( DATEPART(mi, TalkTime) * 60 ) + DATEPART(ss, TalkTime)), 0))
FROM [CRMIntegration].[dbo].[Five9CallLog]
where AGENT_NAME = 'XYZ'
and DateOfcall between
DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) and dateadd(day,datediff(day,1,GETDATE()),0)
result of the query is '23:34:38.0000000',but this should be always less than or equal to the first result
Is the function i am using for calculating total talk time is correct?
I suspect your first result is greater than 1 day, but that is being truncated by using TIME. Instead of adding seconds to 0 and converting to TIME, you could use DATEDIFF() to get total minutes:
SELECT DATEDIFF(MINUTE,0,TalkTime)
Ideally you'd have a start/end time to use instead of 0, because you'll run into an overflow if you need to get too precise and try starting from 0. If it's always relative to the start of the day, you can use:
SELECT DATEDIFF(SECOND,CAST(TalkTime AS DATE),TalkTime)
Likewise, you could simply remove the TIME conversion from what you already have.
I've a query having lot of datetime as result.
Suppose I want to count how much of these datetime are in a time slot like:
07:00 to 08:00
08:01 to 09:00
so I want something like this:
_______________________
| slot | count
|______________|________
| 7:00 - 8:00 | 10
| 8:01 - 9:00 | 2
I really don't know what i can do to have this result.
I've tried something like this but obv i get error:
SELECT something
FROM something
WHERE
CAST(DATEPART(hour, <data>) as varchar(2)) + ':' + CAST(DATEPART(minute, <data>)as varchar(2)) between <slot1> and <slot2>
TIPS: probably I'll get the time slots dynamically from the report services.
Here is query to accomplish what you seem to looking for:
SELECT CAST(DATEPART(HH, time) AS VARCHAR(2))+':00 - '+CAST(DATEPART(HH, time)+1 AS VARCHAR(2))+':00', COUNT(*)
FROM your_table
GROUP BY DATEPART(HH, time)
Here is code at SQL Fiddle
[EDIT]:
AS per the new requirement, below query can accomplish what you want:
SELECT (CAST(DATEPART(HH, time) AS VARCHAR(2)) +
CASE WHEN DATEPART(MI, time) <= 30 THEN ':00 - '+CAST(DATEPART(HH, time) AS VARCHAR(2))+':30'
ELSE ':30 - '+CAST(DATEPART(HH, time)+1 AS VARCHAR(2))+':00' END) AS r,
COUNT(*)
FROM your_table
GROUP BY
(CAST(DATEPART(HH, time) AS VARCHAR(2)) +
CASE WHEN DATEPART(MI, time) <= 30 THEN ':00 - '+CAST(DATEPART(HH, time) AS VARCHAR(2))+':30'
ELSE ':30 - '+CAST(DATEPART(HH, time)+1 AS VARCHAR(2))+':00' END)
Here is the code at SQL Fiddle
If you do not want to use CASE statement two times as in the above query then you can do a GROUP BY in an outer query enclosing the current as an inner query as below:
SELECT r, COUNT(*) AS cnt FROM
(
SELECT (CAST(DATEPART(HH, time) AS VARCHAR(2)) +
CASE WHEN DATEPART(MI, time) <= 30 THEN ':00 - '+CAST(DATEPART(HH, time) AS VARCHAR(2))+':30'
ELSE ':30 - '+CAST(DATEPART(HH, time)+1 AS VARCHAR(2))+':00' END) AS r
FROM your_table
) tab
GROUP BY r
Here is the code at SQL Fiddle
Let me know if it solved your problem.