I have a database/table in SQLITE using the following structure
CREATE TABLE milestones (
ID INT( 10 ) NOT NULL,
Title VARCHAR( 50 ) DEFAULT NULL,
mYear INT( 11 ) NOT NULL,
mMonth INT( 11 ) DEFAULT NULL,
mDay INT( 11 ) DEFAULT NULL,
mText VARCHAR( 2000 ) NOT NULL,
Theme1 VARCHAR( 50 ) DEFAULT NULL,
Theme2 VARCHAR( 50 ) DEFAULT NULL,
ImageURL VARCHAR( 50 ) DEFAULT NULL,
PRIMARY KEY ( ID )
);
I am using the following query to get the 5 nearest dates in relationship to today’s date.
SELECT dtable.ID, dtable.Date
FROM(SELECT ID, date(mYear||'-'||mMonth||'-'||mDay) as Date
FROM milestones
WHERE mMonth IS NOT NULL AND mDay IS NOT NULL AND mMonth ="+varMonth+")AS dtable
ORDER BY ABS("+varDay+"-date(dtable.Date, '%d'))
LIMIT 5;
The problems using this query is that I am only getting records with the date format yyyy-mm-dd.
If there is any record with 1 digit month or day ( the format yyyy-m-d or yyyy-mm-d or yyyy-m-dd) it doesn’t show in the result.
How can I solve this problem without changing the datatype of mMonth or mDay?
If you want to pad a string with zeros to be sure it is 2-char wide, you can use this simple dirty trick with the few SQLite builtin available functions: