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.
Related
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.
I have a table for PERSON and in this they have a Name and a Birthday.
For example, I have a table called PERSON
INSERT INTO PERSON VALUES ( 'alice', '1986-10-10');
INSERT INTO PERSON VALUES ( 'kate', '1992-10-10');
I would like to type in a query that will produce alice and kate ( A query that displays all entries that share month and day, the year doesn't matter). Birthday is used to store this date and I used the Date data type.
I have other values with different birthdates, so I can't seem to figure out the query that will produce alice and kate (because they share the month and day of their birthday)
This is what I have so far, but this is not producing anything:
SELECT Name FROM PERSON
WHERE Birthday LIKE '_____XX__XX';
Your query will work as is (with the corrected pattern), but you need to ensure that your dates are all in the correct format. From the documentation:
A time string can be in any of the following formats:
1. YYYY-MM-DD
...
So you need to use that format for all dates. "1992-3-3" will not be recognized as a date; you need to use "1992-03-03". Or you can use julian dates which makes arithmetic very convenient but reading things a little harder without use of the date/time functions.
If you use the correct ISO date format, then your LIKE clause will work, or you could use the date/time functions to extract a piece of the date., e.g.,
SELECT name, strftime('%m-%d',birthday) FROM person;
alice|10-10
kate|10-10
so if you wanted you could GROUP BY that term:
SELECT strftime('%m-%d',birthday) as mday, count(*) FROM person GROUP BY mday;
Or find all distinct values, or whatever you prefer.
For example, you can get a list of all people and all the people sharing birthdays with them by joining Person to itself:
SELECT p1.name, p2.name, strftime('%m-%d', p1.birthday)
FROM person p1 LEFT OUTER JOIN person p2
ON (strftime('%m-%d',p1.birthday) = strftime('%m-%d',p2.birthday) AND p1.rowid <> p2.rowid) ;
That is, join the table to itself using the extracted month-day and exclude joining the row to itself. Using a left outer join includes people that don't share a birthday with anyone.
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.
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
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