I have table containing branch id and branch starttime and endtime
BRANCHID SHIFTID STARTTIME STOPTIME
1 1 1900-01-01 00:01:00.000 1900-01-01 23:58:00.000
4 4 1900-01-01 07:30:00.000 1900-01-01 18:00:00.000
5 5 1900-01-01 06:30:00.000 1900-01-01 19:00:00.000
6 6 1900-01-01 06:30:00.000 1900-01-01 17:00:00.000
7 7 1900-01-01 00:30:00.000 1900-01-01 18:00:00.000
Now i want to get the number of hours in date range like :
BRANCHID Hours
1 1
1 2
1 3
. .
. .
The resultant table containing branch id and hours in time interval. like branch 1 start time is 00:01:00 - 23:58:00. Than branch id 1 time interval contains following hours 1,2,3,4,5,6 and so on..
select
branchid from table t1
cross apply
(
select n from numbers
where n >=datepart(hour,starttime) and n<=datepart(hour,stoptime)
) b
Below are some links on the numbers table used..
1.http://dataeducation.com/you-require-a-numbers-table/
2.https://dba.stackexchange.com/questions/11506/why-are-numbers-tables-invaluable
Create temp table #hours.
create table #Hours
(
BId int,
[Hour] int
)
Get max and min datetime.
While (#minDateTime < #maxDateTime)
begin
insert into #Hours values (#brID,Datepart(hour,#minDateTime))
set #minDateTime = DATEADD(hh,1,#minDateTime)
end
Select * from #Hours
Related
I have a table1 which stores the data for multiple dates say (between 2022-01-01 to 2022-01-10) and I have another table2 which has time interval and has every 5mins information of dates available in table1 (i.e. 288 records for each day in above table).
Now, How I can write a query for table 2 to calculate time interval for each specific date in table 1. Say I need time intervals between
2022-01-01 12:00:00 to 2022-01-01 02:00:00
2022-01-02 00:05:00 to 2022-01-02 23:00:00
2022-01-04 00:05:00 to 2022-01-10 15:00:00
I tried using DATEDIFF function but that is not giving the results. let's say If I take date 2022-01-02 00:00:00 then my time interval should go back to 1 it should be 2 for 2022-01-02 00:05:00
Below is the example of data:
Table 1:
ID Start date End date
20030917.D0001 2003-09-17 14:10:00 2003-09-18 14:20:00
Table 2:
Date Time interval Amount
2003-09-17 1 150
2003-09-17 2 100
2003-09-17 3 200
2003-09-17 288 250
2003-09-18 1 250
2003-09-18 2 300
2003-09-18 3 1100
2003-09-18 288 150
The time interval in table 2 is every 5 mins of that particular date. Now I need to fetch the data from table 2 which matches with specific date and time in table1
i'm guessing here, if you are looking to return data from table2 with specific time and the table formatted like below
table1
tbl1id
Date
1
2022-01-01
2
2022-01-10
table2
tbl2id
tbl1id
Time
1
1
12:00:00
2
1
12:05:00
3
1
12:10:00
your query should be
select *
from table2
where Time between '12:00:00' and '02:00:00'
and tbl1id
in
(
select tbl1id
from table1
where Date = '2022-01-01'
)
however it you are looking for just looking for calculate time interval in Minute
DECLARE #startdate DATETIME2 = '2022-01-02 00:00:00';
DECLARE #enddate DATETIME2 = '2022-01-02 00:05:00';
SELECT DATEDIFF(Minute, #startdate, #enddate);
that should do it.
I have a code that traces a number of week of the date in a period that set by parameters
CREATE TABLE #test
(
job int,
dateL datetime
)
INSERT INTO #test
VALUES (1, '2021-10-04'),
(2, '2021-10-05'),
(3, '2021-10-11'),
(4, '2021-10-12')
DECLARE #startdate datetime = '2021-10-05',
#enddate datetime = '2021-12-03'
SELECT
dateL,
(CASE
WHEN t.DateL BETWEEN #startDate and #endDate
THEN (DATEDIFF(wk,#startDate, t.DateL)) + 1
ELSE -1
END) AS WeekNumber
FROM
#test t
DROP TABLE #test
The results of this
dateL WeekNumber
---------------------------------------
2021-10-04 00:00:00.000 -1
2021-10-05 00:00:00.000 1
2021-10-11 00:00:00.000 2
2021-10-12 00:00:00.000 2
But it now quite what I need, and I don't understand how to set this to start count from #startDate.
So it should count like:
05.10 - 11.10 - first week
12.20 - 18.10 - second week
and so on,
So it would look like that
dateL WeekNumber
-----------------------------------
2021-10-04 00:00:00.000 -1
2021-10-05 00:00:00.000 1
2021-10-11 00:00:00.000 *1*
2021-10-12 00:00:00.000 2
I tried to set ##datefirst to
datepart(weekday, #startDate)
but it just ignores me.
Could someone may be recommend something, thanks!
Each row has a DateEff and a DateExp. Lets say I return 5 rows. I need to check the DateEff from the first row to see if it is in between the DateEff and DateExp of the second, third, fourth and fifth row and so on. I need to check every DateEff to make sure it is not between any rows DateEff and DateExp.
Here is a sample of what the data looks like. As you can see, row 3 DateEff is (2013-03-30) and it is in between row 4 DateEff and DateExp and row 5DateEff and `DateExp.
Table
rowid DateEff DateExp
1 1969-01-01 2012-09-30
2 2012-10-01 2012-12-31
3 2013-03-30 2014-12-31
4 2013-01-01 2015-02-10
5 2013-01-01 2999-01-01
Results would look like this
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
I think this should work for you:
select
[Prob Id] = t.rowid,
[Problem Date] = t.DateEff,
[Affected Id] = a.rowid,
[Aff Date Range] = concat(a.DateEff,' - ',a.DateExp)
from tbl t -- your table is called tbl
outer apply
(
select *
from tbl -- your table is called tbl
where t.DateEff between dateeff and DateExp and rowid > t.rowid
) a
where a.DateEff is not null
order by t.rowid, t.DateEff;
With your sample data this is the result:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
4 2013-01-01 5 2013-01-01 - 2999-01-01
To get the exact output from your example (excluding row 4) change the condition in the apply to t.DateEff > dateeff and t.DateEff < DateExp and rowid > t.rowid. The output would then be:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
Join the table to itself to return overlapping pairs of rows:
Select a.RowID, a.DateEff as ProblemDate
, b.RowID as OverlapID, b.DateEff as OverlapStart, b.DateExp as OverlapEnd
from MyTable a
left join MyTable b
on a.RowID <> b.RowID
and a.DateEff <= b.DateExp
and a.DateEff >= b.DateEff
I have a table that contains a DateField(DataType : DateTime) and TimeField(DataType : Float)
My output should be DateTime . My tables are in SQL Server 2008
Here is an example :
Table A
ID StartDate StartTime
1 2012-06-08 00:00:00.000 1223
2 2012-08-07 00:00:00.000 910
3 2012-05-02 00:00:00.000 1614
4 0094-07-13 00:00:00.000 1245
5 1994-04-18 00.00:00.000 2573
I need to get my output in such a way that I should it should validate for the correct time and correct date and append these two and insert into table B
Table B :
ID StartDateTime
1 2012-06-06 12:23:00.000
2 2012-08-07 09:10:00.000
3 2012-05-02 16:14:00.000
Note that I intentionally left rows 4 and 5 out of the result set; these rows should be ignored because they don't contain valid datetime or time data.
Have you considered correcting the design, and storing the date/time together, or at least storing date and time using the proper data types? In the meantime:
SELECT StartDate + STUFF(RIGHT('0' + RTRIM(StartTime), 4), 3, 0, ':')
FROM dbo.table
WHERE ISDATE(StartDate) = 1 AND CONVERT(INT, StartTime) < 2400
-- wow what a bunch of absolute garbage data you have
-- what Government agency are you paying to provide this data?
AND CONVERT(INT, StartTime) % 100 BETWEEN 0 AND 59;
I have a sql query that is grouping rows by calendar week
select count(*),datepart(wk,mydate)
from MyTable
where mydate between '12/26/2010' and '1/8/2011'
group by datepart(wk,mydate)
The date range is two weeks but three rows come back because Jan 1 is a saturday and is the only day in the range that DATEPART returns a 1 the other dates return 53 or 2.
I want jan 1 to be grouped with the dates that return a 53, but I want it to be a generic answer not something like CASE WHEN datepart(wk,mydate) = 53 then 1 else datepart(wk,mydate) end because that will work for that specific date range not for other years.
I'm just wondering what a good solution would be
thanks in advance.
We use to choose as week of a date, the week of his last sunday (first day of the week in SQL). So, for each date, you can ask for the week of his last sunday with the following query:
select count(*),datepart(wk,mydate-DATEPART(dw,mydate)+1)
from MyTable
where mydate between '12/26/2010' and '1/8/2011'
group by datepart(wk,mydate-DATEPART(dw,mydate)+1)
Perhaps you can use iso_week instead of wk.
select count(*),datepart(iso_week,mydate)
from MyTable
where mydate between '12/26/2010' and '1/8/2011'
group by datepart(iso_week,mydate)
Sample:
declare #T table (Val datetime)
insert into #T values
('2010-12-30'),
('2010-12-31'),
('2011-01-01'),
('2011-01-02'),
('2011-01-03'),
('2011-01-04'),
('2011-01-05')
select
Val,
datepart(iso_week, Val) as ISO_WEEK
from #T
Result:
Val ISO_WEEK
----------------------- -----------
2010-12-30 00:00:00.000 52
2010-12-31 00:00:00.000 52
2011-01-01 00:00:00.000 52
2011-01-02 00:00:00.000 52
2011-01-03 00:00:00.000 1
2011-01-04 00:00:00.000 1
2011-01-05 00:00:00.000 1
Try DateDiff() instead with your start date as the date to compare.