Satheesh Francis
Satheesh Francis

Reputation: 1

How to do SQLite date comparison if month and year are stored in separate columns as integer values

My SQLite table structure is as follows:

Table: Income. 
Columns:   IncomeID, IncomeAmount,  IncomeMonth, IncomeYear

How can i write a SQLite query to retrieve income Amounts between a given date range.

I know, we need to use date function, but i dont know how to specify two columns in it, i mean IncomeMonth and IncomeYear

Upvotes: 0

Views: 823

Answers (1)

CL.
CL.

Reputation: 180080

If you know the year and month of the given date range, you do not need date functions.

A record is in the range if its year is after the range's start year, or, if both are in the same year, if the record's month is the same or after the range's start month. The comparions for the range end are similar:

SELECT *
FROM Income
WHERE ( IncomeYear > StartYear OR
       (IncomeYear = StartYear AND IncomeMonth >= StartMonth))
  AND ( IncomeYear < EndYear OR
       (IncomeYear = EndYear   AND IncomeMonth <= EndMonth))

Upvotes: 4

Related Questions