I am struggling to find a working query in SQLite, that will return my records in their upcoming birthday along with the number of days it will happen
CREATE TABLE contact_birthday ('contact_id' varchar,'data1' varchar,'display_name' varchar)
data1 holds the birthday in YYYY-MM-DD format
Something like:
"1986-06-28","Angel","0"
"1979-06-29","Bea","1"
"1984-07-02","John","4"
"1984-06-26","Mark","364"
I’ve found some other results but they are working for MySQL, or SQL Server not in SQLite.
How would be the query in SQLite without user defined functions?
I got it to work using julianday (the only way to get a ‘day’ count AFAIK)
My result:
Quick explanation:
julianday(strftime('%Y', 'now')<- this pulls the current year so that julianday for data1 doesn’t consider years from the 80’s||strftime('-%m-%d', data1))<- this concatenates the month/day for the actual birthday to the current yearwhere birthday between -1 and 30;<- the -1 ensures you will see results all the way up through ‘today’. The 30 is the maximum amount of days you want to see in advance.Edit 1:
Here’s a modification that uses seconds since 1970 epoch rather than julian day:
Result:
Edit 2: [Pentium10]
includes proper localtime
Edit 3: [Pentium10]
includes the order by if from today to next year this time are exactly 365 days
** still needs the dynamic calculation of 365 day, as that can be 366 for leap years
Edit 4: [Matt]
This actually might work for you:
Edit 5: [Pentium10]
THE ANSWER
Result:
So birthdays that are coming up in the current year will be negative (e.g. John’s is 3 days in the FUTURE), birthdays that have already passed this year will be positive.
This should allow for the correct ordering.