I'm trying to add multiple record of datetime in order to get a "Total Time".
How can I achieve that ?
2000-01-01 00:01:50.000
2000-01-01 00:02:05.000
2000-01-01 00:03:58.000
2000-01-01 00:04:54.000
Total should be : 00:12:57
SELECT DATEADD(second,
SUM(DATEDIFF(second,'20000101', YourIimes)),
'20000101')
AS TotalTime From YourTable
You can do :
select dateadd(ss, sum(datediff(ss, 0, cast(dt as time))), 0) as totaltime
from table t;
here is a test table with the data to evaluate the logic
DECLARE #Records table (Duration datetime)
INSERT INTO #Records
SELECT '2000-01-01 00:01:50.000' UNION ALL
SELECT '2000-01-01 00:02:05.000' UNION ALL
SELECT '2000-01-01 00:03:58.000' UNION ALL
SELECT '2000-01-01 00:04:54.000'
SELECT
[Total Time]=convert(varchar(10), dateadd(second, SUM(DATEDIFF(SECOND,'20000101', Duration)), 0), 108)
FROM
#Records
Related
I have a table in which one column as registrationDate whose type is datetime. I need to find all the rows where registrationDate as timestamp as 00:00:00.000.
for example :
registrationDate: '2019-03-20 00:00:00.000'
I need to query something similar as below :
select * from table where registrationDate like '%00:00:00.000';
You can cast datetime as time:
WHERE CAST(registrationDate AS TIME) = '00:00'
WHERE registrationDate = CONVERT(date, registrationDate);
See UPDATE below
Create Table #tbl
(
registrationDate Datetime
)
Insert Into #tbl Values
('2019-03-20 00:00:00.000'),
('2019-03-20 00:00:25.000')
Query
Select * From #tbl
Where Cast(registrationDate As Time) = '00:00:00.0000000'
Result:
registrationDate
2019-03-20 00:00:00.000
Update: If you really need to use "Like"
Select * From #tbl
Where convert(VarChar(50), registrationDate, 121) Like '%00:00:00.000'
select * from table where FORMAT(registrationDate, 'HH:mm:ss:ms') ='00:00:00.000'
The following expression gets for every datetime value the corresponding date with time portion:
dateadd(day, datediff(day, 0, '<date_time>'), 0)
Hence, the following will do the job:
create table foo (id int, registrationDate datetime)
insert foo values
(1, '2019-04-04T03:22:48.00'),
(2, '2019-04-04T00:00:00.00')
select * from foo
where registrationDate =
dateadd(day, datediff(day, 0, registrationDate), 0)
In Microsoft SQL Server 2000, I have this data.
1900-01-01 00:10:10.830
1900-01-01 00:10:10.430
From the above column, I want to select the datetime and round off the milliseconds, in order to get the below output
1900-01-01 00:10:11
1900-01-01 00:10:10
Thanks in advance
For SQL Server 2008 and above, you can do use DATETIME2. DATETIME2 is available in SQL Server 2008 and above - for more info see here:
SELECT CAST('1900-01-01 00:10:10.830' AS DATETIME2(0));
SELECT CAST('1900-01-01 00:10:10.430' AS DATETIME2(0));
Confirmed Output
For earlier version of SQL Sever, for example SQL Server 2000. You can do something like this:
SELECT DATEADD(ms, -DATEPART(ms, DATEADD(ms, 500, CAST('1900-01-01 00:10:10.830' AS DATETIME))) , DATEADD(ms, 500, CAST('1900-01-01 00:10:10.830' AS DATETIME)));
SELECT DATEADD(ms, -DATEPART(ms, DATEADD(ms, 500, CAST('1900-01-01 00:10:10.430' AS DATETIME))) , DATEADD(ms, 500, CAST('1900-01-01 00:10:10.430' AS DATETIME)));
SELECT *
, DateAdd(ss, rounded_second, round_down_seconds) As result
FROM (
SELECT *
, Round(nanoseconds / 1000.0, 0) As rounded_second
FROM (
SELECT the_date
, DatePart(ms, the_date) As nanoseconds
, DateAdd(ss, DateDiff(ss, 0, the_date), 0) As round_down_seconds
FROM (
SELECT '1900-01-01 00:10:10.830' As the_date
UNION ALL
SELECT '1900-01-01 00:10:10.430'
) As x
) As y
) As z
I've split out each step to be as clear as possible.
If you want a single liner:
SELECT the_date
, DateAdd(ss, Round(DatePart(ms, the_date) / 1000.0, 0), DateAdd(ss, DateDiff(ss, 0, the_date), 0)) As result
FROM (
SELECT '1900-01-01 00:10:10.830' As the_date
UNION ALL
SELECT '1900-01-01 00:10:10.430'
) As x
I need to select data based on date and time.I have two criteria.How to implement this.
1)select the data between 1-1-2013 and 1-10-2013 with time rage between 10 to 16
2)select the data between 1-1-2013 and 1-10-2013 with time range between 20 to 8 next morning
I implemented a code.Its only working for first criteria.Here is that code.
where date>='1-1-2013' AND date <'1-10-2013'
AND CAST(date AS TIME) between '10:00' and '16:00'
Here the date field in the table is datetime type.Pleases help to solve this
1)
WHERE Date Between '2013-01-01 10:00' AND '2013-10-01 16:00'
2)
WHERE Date Between '2013-01-01 20:00' AND '2013-10-01 08:00'
Try this:
DECLARE #tmp TABLE ( date DATETIME )
INSERT INTO #tmp
( date )
VALUES ( '2013-01-01 10:09:29' -- date - datetime
)
INSERT INTO #tmp
( date )
VALUES ( '2013-01-01 15:09:29' -- date - datetime
)
INSERT INTO #tmp
( date )
VALUES ( '2013-01-01 17:09:29' -- date - datetime
)
INSERT INTO #tmp
( date )
VALUES ( '2013-01-01 07:09:29' -- date - datetime
)
SELECT date
FROM #tmp AS t
WHERE CONVERT(DATE,date) >= CONVERT(DATE, '01-01-2013', 105)
AND CONVERT(DATE,date) <= CONVERT(DATE, '01-01-2013', 105)
AND CONVERT(TIME, date) BETWEEN CONVERT(TIME, '10:00')
AND CONVERT(TIME, '16:00')
did i understand correctly? i don't know..
try this
where date>='1-1-2013' AND date <'1-10-2013'
AND ((CAST(date AS TIME) between '10:00' and '16:00')
OR (CAST(date AS TIME) between '20:00' and '23:59')
OR (CAST(date AS TIME) between '00:00' and '08:00'))
So what I am trying to is generate all the hours that are inside a specific time range.
So given the range 11 AM to 2:00 PM, I would get:
11:00 AM
12:00 PM
1:00 PM
2:00 PM
I am trying to avoid having to store every specific hour a store might be open and just store the range (I need to compare the hours against other times)
Thanks
No loops, recursive CTEs or numbers table required.
DECLARE
#start TIME(0) = '11:00 AM',
#end TIME(0) = '2:00 PM';
WITH x(n) AS
(
SELECT TOP (DATEDIFF(HOUR, #start, #end) + 1)
rn = ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_columns ORDER BY [object_id]
)
SELECT t = DATEADD(HOUR, n-1, #start) FROM x ORDER BY t;
You could use a recursive CTE. This would generate the hours between 11 and 14:
;with Hours as
(
select 11 as hr
union all
select hr + 1
from Hours
where hr < 14
)
select *
from Hours
Live example at SQL Fiddle.
If you have a numbers table (click the link to create one if you don't)...
create table test(
startTime time
, endTime time
)
insert into test
select '11:00', '14:00'
select
dateadd(hh, n.n, t.startTime) as times
from test t
inner join Numbers n
-- assuming your numbers start at 1 rather than 0
on n.n-1 <= datediff(hh, t.startTime, t.endTime)
If this is specialized, you can create an hours table with just 24 values.
create table HoursInADay(
[hours] time not null
, constraint PK_HoursInADay primary key ([hours])
)
-- insert
insert into HoursInADay select '1:00'
insert into HoursInADay select '2:00'
insert into HoursInADay select '3:00'
insert into HoursInADay select '4:00'
insert into HoursInADay select '5:00'
insert into HoursInADay select '6:00'
insert into HoursInADay select '7:00'
...
select
h.[hours]
from test t
inner join HoursInADay h
on h.[hours] between t.startTime and t.endTime
The easiest way I can think of to do this is to have only 1 permanent table with a list of all hours; 24 entries total.
Create table dbo.Hours (Hourly_Time Time NOT NULL)
Insert into dbo.Hours ...
Then, given times A & B:
select * from dbo.Hours where Hourly_Time<=A and Hourly_Time>=B
#Andomar Thanks a lot, you helped me, there is my add above your code.
*----------------------------
create view vw_hoursalot as
with Hours as
(
select DATEADD(
dd, 0, DATEDIFF(
dd, 0, DATEADD (
year , -5 , getDate()
)
)
) as dtHr
union all
select DATEADD (minute , 30 , dtHr )
from Hours
where dtHr < DATEADD(
dd, 0, DATEDIFF(
dd, 0, DATEADD (
year , +5 , getDate()
)
)
)
)
select * from Hours
----------------------------
select * from vw_hoursalot option (maxrecursion 0)
----------------------------*
I have a datetime field and I want to count all records by day from hour 8:00 to 9:00 in a range of date (i.e: from: 01/01/2012 to: 01/03/2012).
for instance, if I have the below data:
2012-01-01 08:26
2012-01-01 08:40
2012-01-01 09:26
2012-01-01 10:26
2012-01-02 08:06
2012-01-02 09:26
2012-01-02 09:40
2012-01-03 08:30
2012-01-03 10:26
the result should look like this:
2012-01-01 2
2012-01-02 1
2012-01-03 1
EDIT:
Here is what I got using #gbn solution (it works for me)
SELECT COUNT(*), convert(varchar(10),ScanDate,101)
FROM tblSingleBox (nolock)
WHERE DATEPART(Hour, scandate) = 8
and (scandate>='01/03/2012' and scandate<'01/05/2012')
GROUP BY convert(varchar(10),ScanDate,101)
order by convert(varchar(10),ScanDate,101)
SQL Server 2005 and earlier
SELECT
COUNT(*),
DATEADD(dd, DATEDIFF(dd, 0, SomeDateTime), 0)
-- SQL Server 2008: CAST(SomeDateTime AS date)
FROM
SomeTable
WHERE
DATEPART(Hour, SomeDateTime) = 8
GROUP BY
DATEADD(dd, DATEDIFF(dd, 0, SomeDateTime), 0);
-- SQL Server 2008: CAST(SomeDateTime AS date);
MySQL:
SELECT
COUNT(*),
DATE(SomeDateTime)
FROM
SomeTable
WHERE
HOUR(SomeDateTime) = 8
GROUP BY
DATE(SomeDateTime);
In this case the simplest code and the fastest code will likely vary a lot, and possibly be dependent on the size of your data set.
I suspect the fastest would be to join on a calendar and filter. In this case I'll make a calendar before I start, but I suggest a permanent one...
CREATE TABLE #calendar (
date AS DATETIME
)
INSERT INTO #calendar SELECT '2011-12-31' UNION ALL SELECT '2012-01-01'
UNION ALL SELECT '2012-01-02' UNION ALL SELECT '2012-01-03'
UNION ALL SELECT '2012-01-04' UNION ALL SELECT '2012-01-05'
SELECT
calendar.date,
COUNT(*)
FROM
yourTable
INNER JOIN
#calendar AS calendar
ON yourTable.DateTimeField >= calendar.date + #minTime
AND yourTable.DateTimeField < calendar.date + #maxTime
WHERE
calendar.date >= #startDate
AND calendar.date <= #endDate
GROUP BY
calendar.date
This should yield the most index friendly query, and so for any reasonable size of data be very efficient.
NOTE:
The calendar.date + #Time sections will vary. You just said MS SQL Server, so it would be more like...
ON yourTable.DateTimeField >= DATEADD(hour, #minHour, calendar.date)
AND yourTable.DateTimeField < DATEADD(hour, #maxHour, calendar.date)
SQL Server 2005
IF OBJECT_ID('tempdb.dbo.#DATES') IS NOT NULL
DROP TABLE #DATES
CREATE TABLE #DATES
( LogDate smalldatetime )
INSERT INTO #DATES
( LogDate )
SELECT '2012-01-01 08:26'
UNION SELECT '2012-01-01 08:40'
UNION SELECT '2012-01-01 09:26'
UNION SELECT '2012-01-01 10:26'
UNION SELECT '2012-01-02 08:06'
UNION SELECT '2012-01-02 09:26'
UNION SELECT '2012-01-02 09:40'
UNION SELECT '2012-01-03 08:30'
UNION SELECT '2012-01-03 10:26'
UNION SELECT '2012-01-03 15:26'
DECLARE #nStartHour smallint
DECLARE #nEndHour smallint
SET #nStartHour = 15
SET #nEndHour = 16
SELECT CONVERT(varchar, LogDate, 101), COUNT(*)
FROM #DATES
WHERE DATEPART(hour,LogDate) BETWEEN #nStartHour AND (#nEndHour - 1)
GROUP BY CONVERT(varchar, LogDate, 101)