Getting last 24 hours from current time in sql - sql

I am not sure why the dateadd function is not working here. I am trying to pull only the last 24 hours from current time but i see hours like 3-6 pm of today's date. the data type is datetime but i am not sure what is going on here.
select Name, location, myDate from myTable where myDate >= DATEADD(hh, -24, GETDATE())
when i run the query above the outcome will include this:
2015-03-05 15:00:00.000
2015-03-05 15:30:00.000
2015-03-05 16:00:00.000
2015-03-05 16:30:00.000
2015-03-05 17:00:00.000
2015-03-05 17:30:00.000
2015-03-05 18:00:00.000
2015-03-05 18:30:00.000
2015-03-05 19:00:00.000
2015-03-05 19:30:00.000
2015-03-05 20:00:00.000
2015-03-05 20:30:00.000
2015-03-05 21:00:00.000
2015-03-05 21:30:00.000
2015-03-05 22:00:00.000
2015-03-05 22:30:00.000
2015-03-05 23:00:00.000
2015-03-05 23:30:00.000
I was expecting not to see these hours at all.

Use BETWEEN, ie
select Name, location, myDate from myTable where myDate between DATEADD(hh, -24, GETDATE()) and GETDATE()
This myDate >= DATEADD(hh, -24, GETDATE()) gets you all records where myDate is greater than 24 hours ago, including records that have future dates(if they are correct to have future dates is another story...)

Related

get time series in 8 hours of interval

I am generating one time-series from using the below query.
SELECT * from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::timestamp),
'2021-12-13 10:01:38'::timestamp,
concat(480, ' minutes')::interval) as t(time_ent)) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and it will give me output like below.
2021-11-13 18:00:00.000
2021-11-14 02:00:00.000
2021-11-14 10:00:00.000
2021-11-14 18:00:00.000
2021-11-15 02:00:00.000
but I need output like.
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
currently, the time series hours depend upon the timestamp that I pass. in above it gives me hours like 02,10,18...but I want the hours like 00,08,16...hours should not depend on the time I passed in query. I tried many things but not any success.
as your start of generate_series is set to 10:00:00, so your next step will be 18:00:00
you have to start your serie from 00:00:00 (cast to date) e.g.:
SELECT
time_ent::timestamp without time zone
from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::date),
'2021-12-13 10:01:38'::timestamp ,
concat(480, ' minutes')::interval) as t(time_ent)
) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and the result will be:
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
2021-11-15 08:00:00.000

Select between two dates when there is overlapping

I have these data in my view
ID event_start event_end
499 2021-07-13 00:00:00.000 2021-07-13 00:00:00.000
499 2021-07-15 00:00:00.000 2021-07-15 00:00:00.000
499 2021-07-14 05:00:00.000 2021-07-14 06:00:00.000
499 2021-07-14 02:00:00.000 2021-07-14 03:00:00.000
499 2021-07-14 07:00:00.000 2021-07-14 09:00:00.000
So I have parameters as #date_start and #date_end and would like to select like this:
DECLARE #date_start datetime2 = '2021-07-14 07:00', #date_end datetime2 = '2021-07-14 08:10'
Select ID, t1.event_start, t1.event_end from Table1
WHERE event_start >= #date_start AND event_end <= #date_end
AND ID=499
That will give me no returned result. But my goal is to get the last row.
499 2021-07-14 07:00:00.000 2021-07-14 09:00:00.000
since this row starts at 7 and finishes at 9 which will cover the #date_start and #date_end period. How could I do that?
Also if I changed #date_start and #date_end to this:
DECLARE #date_start datetime2 = '2021-07-14', #date_end datetime2 = '2021-07-14'
It should return
499 2021-07-14 05:00:00.000 2021-07-14 06:00:00.000
499 2021-07-14 02:00:00.000 2021-07-14 03:00:00.000
499 2021-07-14 07:00:00.000 2021-07-14 09:00:00.000
because all three row happened on the same day but just different time.
Thank you
It seems you actually want to check for any overlaps of the date ranges in the table with the date range specified in the parameters.
Assuming that it doesn't matter if two intervals butt-end each other (say 07:00-08:00 and 08:00-09:00), you need to do an interval check like this start1 < end2 AND end1 > start2
DECLARE #date_start datetime2 = '2021-07-14 07:00', #date_end datetime2 = '2021-07-14 08:10'
SELECT
ID,
t1.event_start,
t1.event_end
FROM Table1
WHERE event_start < #date_end AND event_end > #date_start
AND ID = 499;
First thing to bear in mind, are your datetimes in DATETIME type?
If so, let's check the first case.
1.To see whether a datetime/timestamp is contained in a range, we specify the following WHERE clause:
WHERE
event_start <= '2021-07-14 07:00:00' and
event_end >= '2021-07-14 08:10:00'
1. OUTPUT
id event_start event_end
--- ----------------------- -----------------------
499 2021-07-14 07:00:00.000 2021-07-14 09:00:00.000
In case your data is not in DATETIME/TIMESTAMP type, this might not work.
2.Here, you change the condition, since you want all the stuff that happened during a whole day. Therefore, the WHERE clause looks like this:
WHERE
event_start >= '2021-07-14' and
event_end < '2021-07-15'
2. OUTPUT
id event_start event_end
--- ----------------------- -----------------------
499 2021-07-14 05:00:00.000 2021-07-14 06:00:00.000
499 2021-07-14 02:00:00.000 2021-07-14 03:00:00.000
499 2021-07-14 07:00:00.000 2021-07-14 09:00:00.000
Hope it helped!

get all date values between two times in sql?

Table1:
empid DT
9999 2018-10-23 19:00:00.000
9999 2018-10-24 07:00:00.000
9999 2018-10-21 08:00:00.000
9999 2018-10-22 06:00:00.000
9999 2018-10-24 03:00:00.000
9999 2018-10-24 05:00:00.000
9999 2018-10-23 06:00:00.000
9999 2018-10-23 21:00:00.000
Table 2:
emp_id in_time out_time
9999 2018-10-21 08:00:00.000 2018-10-22 06:00:00.000
9999 2018-10-22 06:00:00.000 2018-10-22 07:00:00.000
9999 2018-10-23 19:00:00.000 2018-10-24 07:00:00.000
I tried this query to get all data in table1 between intime and outtime from table2 but i cant able to get in between these in_time and out_time
select a.emp_reader_id, DT, EVENTID1
from trnevents a
left outer join daily_attendance_data d on d.emp_reader_id = a.emp_reader_id
left outer join employee e on e.emp_reader_id = a.emp_reader_id
where (CONVERT(VARCHAR(26), A.DT, 23) >= CONVERT(VARCHAR(26), '2018-10-23', 23)
and CONVERT(VARCHAR(26), A.DT, 23) <= CONVERT(VARCHAR(26), '2018-10-24', 23))
and a.emp_reader_id=9999
group by a.emp_Reader_id, a.dt,a.eventid
order by emp_reader_id, DT asc
Expected Output:
in_time:2018-10-23 19:00:00.000
out_time:2018-10-24 07:00:00.000
2018-10-23 19:00:00.000
2018-10-24 03:00:00.000
2018-10-24 05:00:00.000
2018-10-24 07:00:00.000
if DT is datetime then below where condition will work no need any varchar conversion
where DT>='2018-10-23 19:00:00.000' and DT<='2018-10-24 07:00:00.000'

Unable to set a condition in SQL Server 2008 R2

I have the following data set in the test table:
create table test
(
columndate date,
columntime datetime
)
insert into test values('2014-01-01','22:00:00')
insert into test values('2014-01-02','06:00:00')
insert into test values('2014-01-03','23:00:00')
insert into test values('2014-01-04','05:00:00')
insert into test values('2014-02-01','10:00:00')
insert into test values('2014-02-01','13:00:00')
insert into test values('2014-02-01','15:00:00')
insert into test values('2014-02-01','05:00:00')
columndate columntime
------------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 10:00:00.000
2014-02-01 1900-01-01 13:00:00.000
2014-02-01 1900-01-01 15:00:00.000
2014-02-01 1900-01-01 05:00:00.000
Now I want to show only night timing in the result for example:
columndate columntime
-----------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 05:00:00.000
For which I am trying the following script:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '06:00:00' and '23:00:00'
Note: I will not get the record of timing 05:00:00
But when I use the following script:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00'
Note: I will get expected result but I am getting record of timing 06:00:00 also which I don't want to show.
How can I fix it?
You were really close: instead of BETWEEN you need to use a pair of >= and <=, because you need times outside an interval:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and (cast(columntime as time) <= '05:00:00' OR cast(columntime as time) >= '23:00:00')
Demo.
Does this meet your needs??:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00' and columntime ! ='06:00:00'

Group By,Order by DateTime

I have nearly 15000 data rows with the first column containing date in the format:
2012-05-10 09:00:00.000
I need this data to be sorted by year then month, then day, then hour so for example:
2012-05-10 09:00:00.000
2012-05-10 10:00:00.000
2012-05-10 11:00:00.000
2012-05-10 12:00:00.000
2012-05-11 09:00:00.000
2012-05-11 10:00:00.000
2012-05-11 11:00:00.000
2012-05-11 12:00:00.000
2012-06-01 02:00:00.000
2012-06-01 03:00:00.000
2012-06-01 04:00:00.000
2012-06-01 05:00:00.000
Current SQL Query to do this is below:
SELECT MIN(Datetime)
GROUP BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
HAVING MIN(jmusa_LOG1.DateTime) NOT IN(SELECT DateTime FROM AverageRawData)
ORDER BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
You are describing a normal date sort, so you can just do:
select MyDate
from AverageRawData
order by MyDate
If you don't want duplicates, add DISTINCT like this:
select distinct MyDate
from AverageRawData
order by MyDate
If this does not meet your requirements, please provide sample data used to generate your output example.