The following Microsoft SQL query compares two date fields of a table and returns those records for which the difference in minutes is greater than 5.
SELECT t.Id, t.date1, t.date2,
DATEDIFF(MINUTE, t.date1 , t.date2) AS Mtime
FROM table1 t
WHERE
DATEDIFF(MINUTE,t.date1, t.date2) > 5
I have no idea how to write this with ORACLE. I've searched for solution and the closest I came to was :
SELECT t.date1, t.date2,
(t.date1 - t.date2) * 1440 AS Mtime
FROM table1 t
WHERE
(t.date1 -t.date2) * 1440 > 5
which gives me the error inconsistent datatypes: expected INTERVAL DAY TO SECOND got NUMBER
Does anyone know how to write this query with ORACLE ?
Don't use the difference. Just add the interval:
WHERE t.DeliveryDate >= t.Deadline + interval '5' minute
Or:
WHERE t.DeliveryDate >= t.Deadline + 5 / (24 * 60)
The equivalent in SQL Server is:
WHERE t.DeliveryDate >= DATEADD(minute, 5, t.Deadline)
This is a good habit. If one of the values is a constant, then the use of a function (- or datediff()) prevents the use of an index.
This should work -
SELECT t.Id, t.date1, t.date2,
(CAST(t.date1 AS DATE)-CAST(t.date2 AS DATE)) * 1440 AS Mtime FROM table1 t where (CAST(t.date1 AS DATE)-CAST(t.date2 AS DATE)) * 1440 > 5
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 have thousands of records (rows) that contain a time column.
I can easily get rows with time's in between 4 AM and 5 AM by doing this:
SELECT * FROM MyTable
WHERE CAST(Time AS TIME) BETWEEN '04:00' and '05:00'
To get the rows between 6PM and 4AM:
SELECT * FROM MyTable
WHERE CAST(Time AS TIME) BETWEEN '18:00' and '04:00'
Gives me zero rows..
How can I do this?
Casting a time to a time is strange, but I assume it's actually a Timestamp :-)
You might also use EXTRACT:
SELECT *
FROM MyTable
WHERE EXTRACT(HOUR FROM Time) >= 18
AND EXTRACT(HOUR FROM Time) < 4
Try:
SELECT *
FROM MyTable
WHERE CAST(Time AS TIME) NOT BETWEEN '04:00' AND '18:00'
The values to between (and not between) need to be in order, with the smaller value first. Otherwise, nothing will match.
The expression:
x between a and b
is exactly equivalent to
((x >= a) and (x <= b))
I have a table with over 100,000 rows that contain the following columns: ID, Time, and Boolean.
The time column tracks time down to the second.
I need a query that will find all instances of Boolean = 1 for every 5 minute interval of time from the start of the table to the end, then group the count by time interval.
The table represents 4 hours of data, so I should get 48 rows of results.
I'm using MS SQL Server.
I've tried a few approaches, but the time interval logic is giving me a hard time.
EDIT: I figured it out!
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0), COUNT(*)
FROM table
WHERE isrepeat = 1
GROUP BY by DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0)
ORDER BY by 1
This should do. You can group by your results by an interval of time/5.
select
cast(to_char(Time, 'mi') as int) / 5 * 5 || ' - ' || cast(to_char(Time, 'mi') as int) / 5 * 5 + 5 as "Interval",
count(1)
from tableName
where
Boolean = 1
group by
cast(to_char(Time, 'mi') as int) / 5
select * from tableName where Time
BETWEEN DATEADD(minute, -5, current_timestamp)
AND current_timestamp
I am looking for a where clause that can be used to retrieve records for the last 24 hours?
In MySQL:
SELECT *
FROM mytable
WHERE record_date >= NOW() - INTERVAL 1 DAY
In SQL Server:
SELECT *
FROM mytable
WHERE record_date >= DATEADD(day, -1, GETDATE())
In Oracle:
SELECT *
FROM mytable
WHERE record_date >= SYSDATE - 1
In PostgreSQL:
SELECT *
FROM mytable
WHERE record_date >= NOW() - '1 day'::INTERVAL
In Redshift:
SELECT *
FROM mytable
WHERE record_date >= GETDATE() - '1 day'::INTERVAL
In SQLite:
SELECT *
FROM mytable
WHERE record_date >= datetime('now','-1 day')
In MS Access:
SELECT *
FROM mytable
WHERE record_date >= (Now - 1)
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
MySQL :
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
The INTERVAL can be in YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
For example, In the last 10 minutes
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
Which SQL was not specified, SQL 2005 / 2008
SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())
If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.
in postgres, assuming your field type is a timestamp:
select * from table where date_field > (now() - interval '24 hour');
If the timestamp considered is a UNIX timestamp
You need to first convert UNIX timestamp (e.g 1462567865) to mysql timestamp or data
SELECT * FROM `orders` WHERE FROM_UNIXTIME(order_ts) > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
select ...
from ...
where YourDateColumn >= getdate()-1
SELECT *
FROM tableName
WHERE datecolumn >= dateadd(hour,-24,getdate())
Hello i now it past a lot of time from the original post but i got a similar problem and i want to share.
I got a datetime field with this format YYYY-MM-DD hh:mm:ss, and i want to access a whole day, so here is my solution.
The function DATE(), in MySQL: Extract the date part of a date or datetime expression.
SELECT * FROM `your_table` WHERE DATE(`your_datatime_field`)='2017-10-09'
with this i get all the row register in this day.
I hope its help anyone.
In SQL Server (For last 24 hours):
SELECT *
FROM mytable
WHERE order_date > DateAdd(DAY, -1, GETDATE()) and order_date<=GETDATE()
In Oracle (For last 24 hours):
SELECT *
FROM my_table
WHERE date_column >= SYSDATE - 24/24
In case, for any reason, you have rows with future dates, you can use between, like this:
SELECT *
FROM my_table
WHERE date_column BETWEEN (SYSDATE - 24/24) AND SYSDATE