Eminence Posted May 9, 2018 Posted May 9, 2018 (edited) Hello, I've been searching on how to select the first row in my database which includes a time column based on the latest start_time date for today's date. This is my current code: Local $iCurrentDate = _Now() Local $sqlHandle = _dbOpen($sDbPath) Local $aResult, $iRows, $iCols _SQLite_GetTable2d($sqlHandle, "SELECT start_time FROM " & $srawDb & " ORDER BY datetime(start_time) DESC LIMIT 1", $aResult, $iRows, $iCols) _ArrayDisplay($aResult) _dbClose($sDbpath) What it does is it gets the first row entry in the database however it does not select the latest start_time entry in the database, it always select the first row. I've tried changing DESC to ASC to see if that will do it but the issue still remains. Any thoughts on this? Thanks in advance. *EDIT The date format in the database is by MM/DD/YYYY HH:MM:SS. Edited May 9, 2018 by Eminence
TheXman Posted May 9, 2018 Posted May 9, 2018 I think you answered your question with the additional information that you provided. You need to spend some time understanding the differences between SQLite and other relational databases like MySQL, MS SQL, Oracle, etc. First of all, SQLite does not have a datatype or storage type of DATE. Everything in a SQLite database is stored as either null, text, blob (binary), or a number (real or integer). Second, if you read the sqlite documentation on the datetime function, you will see that "mm/dd/yyyy hh:mm:ss" is not an acceptable time string. If you do not want to have to go thru all sort of conversion gymnastics, it would be much easier to store your dates in a commonly accepted SQLDATE format (YYYY-MM-DD HH:MM:SS) or numeric format like Julian or Epoch. As far as SQLite is concerned, your dates are strings and are being sorted as such. Obviously it cannot sort your dates correctly if the are stored in a MM/DD/YYYY format. You can start learning more about SQLite datatype and date/time functions by going to the following links: https://www.sqlite.org/datatype3.html https://www.sqlite.org/lang_datefunc.html Eminence 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Eminence Posted May 9, 2018 Author Posted May 9, 2018 Oh my bad. Yep, understood now. Thanks @TheXman
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now