I have data that I would like to retrieve from db using the following sql select statement;
B4X:
"SELECT [Mydate], ITEM, DESC FROM MyTable WHERE [Mydate] BETWEEN '10/10/2016' AND '12/31/2016' Order by [Mydate]"
'[Mydate] is date store as string eg. "10/10/2016"
'If my select sql has the same year,I was able to retrieve data eg.
'however if the select statement has difference year , the statement return no data.
'eg. below.
"SELECT [Mydate], ITEM, DESC FROM MyTable WHERE [Mydate] BETWEEN '10/10/2016' AND '01/10/2017' Order by [Mydate]"
SQLLite requires dates to be in YYYY-MM-DD format. Since the data in your database and the string in your query isn't in that format, it is probably treating your "dates" as strings.
As mentioned by Manfred, unless you store the date as: YYYY-MM-DD or YYYYMMDD or ticks, it will be tough to display the results. But if you have no choice but to use your format, then use the SUBSTR(X,Y,Z) function: for instance: for 12/31/2016 you can do: Substr(MyDate, 7,4) '||' Substr(MyDate, 1,2) '||' Substr(MyDate, 4,2)
Hi Mahares,
I tryed your suggestion but got an error.
To convert 12/31/2016 to 2016/12/31 following code works: substr(MyDate, 7,4) || '/' || substr(MyDate, 1,2) || '/' || substr(MyDate, 4,2) AS Date
and WHERE Date BETWEEN '2016/10/10' AND '2017/01/10' ORDER BY Date
Dim MyQuery As String
MyQuery= "SELECT Mydate, ITEM FROM MyTable WHERE (Substr(MyDate, 7,4) || Substr(MyDate, 1,2) || Substr(MyDate, 4,2)) AS DATE" _
& " BETWEEN '20161010' AND '20161231' ORDER BY DATE"
Not sure about sqllite but you can try the CDATE("dd/mm/yyyy") function or encase the date in hashes #dd/mm/yyyy# - must be applied to all dates in the query
@Mahares
My concern was that in your post you have the concatenate operator between quotes which gave me the error. Substr(MyDate, 7,4) '||' Substr(MyDate, 1,2) '||' Substr(MyDate, 4,2)
instead of Substr(MyDate, 7,4) || Substr(MyDate, 1,2) || Substr(MyDate, 4,2)
Of course, your query in post#6 works because you don't have the quotes.
SQLLite requires dates to be in YYYY-MM-DD format. Since the data in your database and the string in your query isn't in that format, it is probably treating your "dates" as strings.
Yes, having just gone through quite a bit of work to get date formats fixed in my project, that can be head scratching.
Having some experience abroad, I have tried to standardize the date format in my projects to "dd-MMM-yyyy" (or "dd MMM yyyy") since that is just about the only format that is most likely to be universally recognized in the world without any ambiguity (ask people in 3 or 4 European countries what does 2017/03/02 mean).
Since SQLite cannot deal with this format directly, I keep the date format in the database as ticks and I do all the conversions outside of SQLite.
Hi klaus:
I am sorry when I posted I did not have a PC to test my little, but now that I tested it, here is exactly how it should be. Tested now and works:
of course you use you own data and column names:
B4X:
Dim MyQuery As String
MyQuery= "SELECT Mydate, col2, col3, (Substr(MyDate, 7,4) || Substr(MyDate, 1,2) || Substr(MyDate, 4,2)) AS DATE" _
& " FROM table1 WHERE DATE BETWEEN '20170217' AND '20170220' ORDER BY DATE"
cursor1=SQL1.ExecQuery(MyQuery)
You can do it yourself with straight string operations. Two major issues are:
1/ what to do about century, eg is 02Feb95 supposed to be 1995 or 2095?
2/ converting 3-letter month to a month number 1..12. Easiest way is "xxJanFebMarAprMayJunJulAugSepOctNovDec".IndexOf("Feb") will return position of the month within the string of all 12 months, and then divide that position by 3 (the leading xx is to pad the position to be a multiple of 3).