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 8814463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:07:46+00:00 2026-06-14T04:07:46+00:00

I’m a beginner to Sqlite and struggling to put together a query for what

  • 0

I’m a beginner to Sqlite and struggling to put together a query for what I need. 🙁
Can anyone help me please?

I have a given date. In my examples below this variable is called ‘givenDate‘ and is of java type Date and therefore in the format yyyy-MM-dd.

I also have a database table called ‘main’ with an ‘ID’ and ‘Date’ column. The Date column entries are in ISO standard String format. So the entries look like this:

ID   Date
001  1949-08-13 06:29:21.000
002  1943-08-13 10:13:45.000
003  1948-08-13 09:12:32.100
004  etc.

I need to get a list of the 10 nearest dates from table ‘main’ when comparing Date column with my given date ‘givenDate’.

So I need to:

  1. Compare each date in the Date column to my given date ‘givenDate’
  2. Work out the Absolute difference between them
  3. Order them by smallest to biggest difference
  4. Return the ID and Date for each of the smallest 10 results

So, if the givenDate is 1948-09-13 then the result would be:

003  1948-08-13 09:12:32.100
001  1949-08-13 06:29:21.000
002  1943-08-13 10:13:45.000
etc

DateDiff doesn’t work in Android as the database is an Sqlite database.

I have tried doing something like this:

String query = 
"SELECT ID, Date" +
"FROM main" +
"WHERE Date IS NOT NULL" +
"ORDER BY ABS(date("+givenDate+") - Date) ASC" +
"LIMIT 10";

This doesn’t seem to work. I just get a list of dates with the same year as the year in the givenDate and in ascending order. The date difference doesn’t seem to actually calculate the difference in date and give the 10 nearest.

I’ve looked through numerous posts but can’t find anything specific to do what I need.

Any help would be greatly appreciated.

Thanks,
Simmy 🙂


The answer was:

Format sqliteDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 

String query2 = 
"SELECT _id, Date " + 
"FROM main " + 
"WHERE Date IS NOT NULL " + 
"ORDER BY ABS(JULIANDAY('" + sqliteDateFormatter.format(aDate) + "') - " + "JULIANDAY(Date)) " + 
"LIMIT 10";
  • 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-06-14T04:07:47+00:00Added an answer on June 14, 2026 at 4:07 am

    According to the documentation, just converting a Date to a string will result in a format that is not understood by SQLite; you’ll have to explicitly use the correct format.

    Furthermore, these date values are just strings, so you cannot simply subtract them. You have to convert them to some numeric date format first.

    Format sqliteDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String query = ... +
        "ORDER BY ABS(JULIANDAY('" + sqliteDateFormatter.format(date) + "') - " +
                     "JULIANDAY(Date))" +
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.