Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

481
Views
Room Database functions WEEK(), MONTH(), etc

I have a room database with a table "Entries" where I have the columns value(int), cat_id(int) and date(Date).

I want to get the sum of all entries of the current month and all entries of the current week. But the selections:

@Query("SELECT sum(value) FROM Entry WHERE cat_id = :cat_id AND WEEK(date, 5) = WEEK(:date, 5) AND YEAR(date) == YEAR(:date)")
float getValueThisWeekByCatId(int cat_id, Date date);

@Query("SELECT sum(value) FROM Entry WHERE cat_id = :cat_id AND MONTH(date) = MONTH(:date) AND YEAR(date) == YEAR(:date)")
float getValueThisMonthByCatId(int cat_id, Date date);

don't work.

This is the error:

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such function: WEEK) float getValueThisWeekByCatId(int cat_id, Date date); Same with function MONTH

Is there another way to that selection?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A little late but I stumbled upon this question because of a similar problem, and your solution with strftime in the answer is correct.

Tried with your table and query and it worked for me:

 CREATE TABLE Entry(
       id int primary key,
       cat_id int,
       value int,
       date date
    );

    insert into entry (id, cat_id, value, date) values (1, 1, 50, '2019-03-19');
    insert into entry (id, cat_id, value, date) values (2, 1, 30, '2019-02-20');
    
    SELECT sum(value) 
    FROM Entry 
    WHERE cat_id = 1 
    AND strftime('%m', date) = strftime('%m', '2019-03-01')
    AND strftime('%Y', date) = strftime('%Y', '2019-03-01')

Also works with CURRENTDATE but obviously have to use current date entries.

Example

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!