Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:34:01+00:00 2026-05-15T12:34:01+00:00

I am struggling to find a working query in SQLite, that will return my

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:34:02+00:00Added an answer on May 15, 2026 at 12:34 pm

    I got it to work using julianday (the only way to get a ‘day’ count AFAIK)

    select name,
    julianday(strftime('%Y', 'now')||strftime('-%m-%d', data1))-julianday('now') as birthday
    from foo
    where birthday between -1 and 30;
    

    My result:

    Angel|-0.479438499547541

    Bea|0.520561488810927

    John|3.52056147716939

    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 year

    where 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:

    select name,
    (strftime('%s',strftime('%Y', 'now')||strftime('-%m-%d', data1))-strftime('%s','now'))/86400.0 as birthday
    from foo
    where birthday between -1 and 30
    order by birthday;
    

    Result:

    Angel|-0.487118055555556

    Bea|0.512881944444445

    John|3.51288194444444

    Edit 2: [Pentium10]

    includes proper localtime

    select data1,display_name,
    (strftime('%s',strftime('%Y', 'now','localtime')||strftime('-%m-%d', data1))-strftime('%s','now','localtime'))/86400.0 as birthday
    from contact_birthday
    order by birthday asc
    

    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

    select data1,display_name,
    ((strftime('%s',strftime('%Y', 'now','localtime')||strftime('-%m-%d', data1))-strftime('%s','now','localtime'))/86400.0+1+365) % 365 as birthday
    from contact_birthday
    order by birthday asc
    

    Edit 4: [Matt]

    This actually might work for you:

    select data1,name,
    365-(strftime('%s',strftime('%Y', 'now', '+1 year', 'localtime')||strftime('-%m-%d', data1))-strftime('%s','now','localtime'))/86400.0 as birthday
    from foo
    order by birthday asc
    

    Edit 5: [Pentium10]
    THE ANSWER

    select data1,display_name,
    ((strftime('%s',strftime('%Y', 'now','localtime')||strftime('-%m-%d', data1))-strftime('%s','now','localtime'))/86400.0+1+((strftime('%s','now', 'localtime','+1 year')-strftime('%s','now',  'localtime'))/86400.0)) % ((strftime('%s','now', 'localtime','+1 year')-strftime('%s','now',  'localtime'))/86400.0) as indays
    from contact_birthday
    order by indays asc
    

    Result:

    1984-07-02|John|-3.65773148148151

    1979-06-29|Bea|-0.657731481481505

    1986-06-28|Angel|0.342268518518495

    1984-06-26|Mark|2.34226851851849

    1987-02-16|Matt|132.342268518519

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.