How to query rows by intraday time range in sqlite - sqlite

I have a column that consists of the datetime for each entry in the table. The entries are across many days and across all times. I want to filter only for entries that happened between a certain time period in the day, like between 10am and 1pm. How does one do that?
The datetimes are stored as a string in ISO-8601 format. Ex: 2021-01-22T12:50:00-05:00

First, keep the sqlite Date and Time Functions doc handy.
Something like:
WHERE strftime("%H:%M",yourdate) between "10:00" and "13:00"
should accomplish the task.

Related

SQLite order by dd:mm

In my sqlite database, birthdays are stored in a string field like: 11-09
I want to order the birthdays but it only looks at the first 2 numbers, I tried:
sql.all('SELECT id,user,date FROM birthdays ORDER BY date DESC LIMIT 4').then(rows => {
how can I make it so that it looks at both day and month?
I would suggest using either a DATE or a DATETIME column for storing birthdays instead. You will have to deal with the date in terms of Unix Time in the rest of your application, but it should simplify dealing with the dates in the long run. Your query would not change from what you have there as well.

SQLite: select all rows made in a specific month

i want to get all entries from a SQLite table, which have the timestamp from the same month.
For example, the user can type in "July" and then i want to get all entries made in the 7. month.
The current "time"-column is a simple string and in the Format (DD.MM.YYYY HH:MM:SS)
Is there a way to do this with SQLite or will i need to use code in my program?
Assuming that your time strings have a fixed length, you could use a query like this:
SELECT * FROM MyTable WHERE time LIKE '__.07%';
However, you should always stored dates in one of the supported date/time formats so that you are able to use the built-int date/time functions.

Query date in Access

I have a database that stores date and time in a single column for each entry as -
8/29/2012 6:09:45 AM - as an example. I am looking for a way to query just today's date all regardless of the time.
Currently I use criteria in a query Like "*9/29/2012*". The issue is that each day that the report is needed the date in the criteria needs to be updated, eliminating the possibility to automate the report.
Is there a way to just to query current date with out have to overhaul the target tables, or having to update the query criteria daily?
Dates are stored as numbers and the integer portion is the date, so you can say:
CInt(MyDate)=Cint(Date)
You can also say:
CDate(Format(MyDate, "yyyy/mm/dd")) = Date()
Or
DateSerial(Year(Mydate),Month(MyDate),Day(Mydate))=Date()
Or
MyDate>=Date And MyDate<Date+1
This final example will take advantage of indexes.
29/09/2012 00:00:00 is equal to Date()
29/09/2012 23:50:00 is greater than Date() but less than Date+1

Time diff calculations where date and time are in seperate columns

I've got a query where I'm trying to get the hours in duration (eg 6.5 hours) between two different times.
In my database, time and date are held in different fields so I can efficiently query on just a startDate, or endDate as I never query specifically on time.
My query looks like this
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(endTime,startTime)),0) FROM events WHERE user=18
Sometimes an event will go overnight, so the difference between times needs to take into account the differences between the dates as well.
I've been trying
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(CONCAT(endDate,' ',endTime),CONCAT(startDate,' ',startTime))),0) FROM events WHERE user=18
Unfortunately I only get errors when I do this, and I can't seem to combine the two fields into a single timestamp.
Pretty sure your problem is that your concatenated values are being sent to TIMEDIFF() as strings rather than DATETIMEs. Try calling the DATETIME function on them:
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(DATETIME(CONCAT(endDate,' ',endTime)),DATETIME(CONCAT(startDate,' ',startTime)))),0) FROM events WHERE user=18
I don't have a MySQL DB in front of my to test that, but I think that or some similar form of it is what you are looking for. There's an example of it in the MySQL docs involving MICROSECOND:
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Edit: Hmm... looks like TIMEDIFF is supposed to work with strings. Worth trying anyway.
TIMEDIFF(endDate,startDate) + TIMEDIFF(endTime,startTime)

How do I pull values between two datetimes at specific interval in MySQL?

I have an application that writes temperature values to a MySQL table every second, It consists of the temperature and a datetime field.
I need to pull these values out of the table at specific intervals, every second, minute, hour etc.
So for example I will need to pull out values between 2 datetime fields and show the temperature at the hour for each of them.
One option I've considered is to create a temporary table that holds a list of the time intervals generated using MySQL INTERVAL and then joining that against the main table.
I'm just wondering if there's some time and date functions in MySQL that I'm overlooking that would make this easier?
Thanks.
You could use between for your date, and then a conditional WHERE clause using time() that looks at the structure of the timestamp. If it has 00:00 (for instance, 16:00:00) within it, take it, if not, leave it.
Example (untested):
SELECT temp, date
FROM temperatures
WHERE (date BETWEEN '2009/01/03 12:00:00' AND '2009/01/04 12:00:00')
AND (time(date) LIKE '%:00:00')
ORDER BY date ASC
LIMIT 10

Resources