0

Im having problem in query i cannot get the proper result dates i want I need to get all the date from today or less than date today

can you help me or give me right query to use

for example this is mytable

id(number) datestring(string)
1          02/06/2019
2          02/06/2019 
3          02/14/2019
4          02/04/2019
5          03/17/2019

query i use: SELECT * FROM mytable WHERE datestring <= date('now')

expected result is id(number) datestring(string) 1 02/06/2019 2 02/06/2019 4 02/04/2019

Thank you for helping

4
  • 1
    You should store your date values in a format understood by sqlite: sqlite.org/lang_datefunc.html Of course comparing a mm/dd/yyyy format string to a yyyy-mm-dd one isn't going to work right. Commented Feb 6, 2019 at 2:57
  • 1
    Also read sqlite.org/datatype3.html#date_and_time_datatype Commented Feb 6, 2019 at 2:58
  • ah hmmm okay let me try it . am also confuse on my format and datetype . :) be rigthback Commented Feb 6, 2019 at 2:58
  • ok decide to adjust my code to proper format. :) thanks @Shawn Commented Feb 6, 2019 at 3:22

1 Answer 1

1

The issue you are encountering is that date('now') will return 2019-02-06 , as such as all rows in the table start with 0 and that this is less than 2.

You either need to convert on of the dates to be in the same format as the other or use the same format when storing the data.

SQLite itself has various date format that can be recognised and used and it is advisable to utilise one of these as reduces necessary complexity.

Time Strings A time string can be in any of the following formats:

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD

SQL As Understood By SQLite - Date And Time Functions

The following is a solution that could be used to convert the datestring column within the query :-

SELECT * FROM mytable WHERE substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2) <= date('now');

The following could be used to convert the existing data to a recognised format :-

UPDATE mytable SET datestring = substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2);

This could be followed by your original query

SELECT * FROM mytable WHERE datestring <= date('now');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.