metrick Active Member Licensed User Longtime User Mar 10, 2017 #1 I have data that I would like to retrieve from db using the following sql select statement; Code: "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]"
I have data that I would like to retrieve from db using the following sql select statement; Code: "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]"
DonManfred Expert Licensed User Longtime User Mar 10, 2017 #2 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. Upvote 0
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.
M Mahares Expert Licensed User Longtime User Mar 10, 2017 #3 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) Upvote 0
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)
klaus Expert Licensed User Longtime User Mar 10, 2017 #4 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 Last edited: Mar 10, 2017 Upvote 0
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
metrick Active Member Licensed User Longtime User Mar 10, 2017 #5 Found a link. Not at home now, and unable to try it. http://stackoverflow.com/questions/4428795/sqlite-convert-string-to-date Upvote 0
Found a link. Not at home now, and unable to try it. http://stackoverflow.com/questions/4428795/sqlite-convert-string-to-date
M Mahares Expert Licensed User Longtime User Mar 10, 2017 #6 klaus said: I tryed your suggestion but got an error. Click to expand... Hi klaus: Did you try the query something like: Code: 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" Upvote 0
klaus said: I tryed your suggestion but got an error. Click to expand... Hi klaus: Did you try the query something like: Code: 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"
S seanfogg Member Licensed User Longtime User Mar 10, 2017 #7 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 Upvote 0
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
klaus Expert Licensed User Longtime User Mar 10, 2017 #8 @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. Upvote 0
@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.
Didier9 Well-Known Member Licensed User Longtime User Mar 10, 2017 #9 DonManfred said: 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. Click to expand... 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. Upvote 0
DonManfred said: 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. Click to expand... 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.
M Mahares Expert Licensed User Longtime User Mar 10, 2017 #10 klaus said: My concern was that in your post you have the concatenate operator between quotes which gave me the error. Click to expand... 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: Code: 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) Upvote 0
klaus said: My concern was that in your post you have the concatenate operator between quotes which gave me the error. Click to expand... 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: Code: 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)
klaus Expert Licensed User Longtime User Mar 10, 2017 #11 The code I posted works also, I tested it, but it's bit more complex with the '/' character in between the substrings. Upvote 0
The code I posted works also, I tested it, but it's bit more complex with the '/' character in between the substrings.
tcgoh Active Member Licensed User Longtime User Feb 3, 2020 #12 Hi, My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Thanks Upvote 0
Hi, My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Thanks
E emexes Expert Licensed User Longtime User Feb 3, 2020 #13 tcgoh said: my datebase is Click to expand... Always makes me smile when typo is a better fit than the intended word ✌ Upvote 0
tcgoh said: my datebase is Click to expand... Always makes me smile when typo is a better fit than the intended word ✌
E emexes Expert Licensed User Longtime User Feb 3, 2020 #14 tcgoh said: My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Click to expand... 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). Upvote 0
tcgoh said: My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Click to expand... 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).
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2020 #15 tcgoh said: My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Click to expand... Please don't post in old threads. Always start a new thread for your question. You can post a link to a similar thread. Upvote 0
tcgoh said: My date string in my datebase is in "02Feb20" format, anyone knows how to convert SQL format? Click to expand... Please don't post in old threads. Always start a new thread for your question. You can post a link to a similar thread.