Sqlite Sort by time not working - android

I am storing date format in sqlite table, I want sort by date from table.
Every record in my SQLite database contains a column which contains a date stored as a string in the format 'yyyy-MM-dd HH:mm:ss'.
I am sharing my table structure. I am using this query to sort by datetime it not sorting by time but sort by date is working fine.
select *
from messages_table
where id = '444'
order by datetime(date_time) asc
I am storing datetime as string in my below table
and I am getting the below wrong sorting by time output please see my date_time column in the picture, anyone guide me.

I have a similar problem with the TIME datatype.
If I enter time values correctly, such as 8:00 or 13:00, they are sorted as strings in an ORDER BY clause, in effect as "800" and "1300", where the string "1300" is sorted before "800" in ascending order.
One solution is to pad all times with a leading 0, so we get "0800" and "1300" which will be sorted time wise correctly.

Related

To fetch records from sqlite table between a range of dates

In my application i am trying to fetch records from a sqlite table with respect to a range of dates selected from the date picker. The records in the table are as follows :
The query formed is as given below :
select * from Order_Master where Order_Date >= '12-04-2015' and Order_Date <= '11-03-2016' And WSS_Code = '1014332'
This query does not return any value which is not the desired result as the dates are in the selected range.
What could possibly be wrong here ? Am i missing something?
Kindly guide me through this. Thanking you in Advance !
Your date format cannot be used for comparisons, because strings are compared lexicographically, i.e., with the first characters compared first.
In this query, you are searching for dates with a month that is at least 12 and, at the same, no larger than 11.
SQLite has no separate data type for dates.
To store dates in a database, you have to choose one of the existing data types (number or text).
When using SQLite's built-in date functions, you must use one of the formats supported by them.
Try this , hope it helps
Change your datatype to text
SELECT * FROM Order_Master WHERE Order_Date BETWEEN '12-04-2015' AND '11-03-2016' AND WSS_Code = '1014332';
As well as your query will also work.

How to select the oldest day, from a column type TEXT in Android SQLITE

I am working with android and SQLITE Database. There I have to Limit the maximum number of records as 10 in one table. That table has a column(dateReg) to insert the date and time when the record is inserted. I am inserting records in to that column in the following format.(dd/MM/yy HH:mm:ss)
I am trying to find the oldest record by the date and to update that record if the row count is 10.
Like to know whether there is a way to select the oldest day by a SQL query, to update that record if the total number of records are 10 in that table.
Thank you very much and Any guidance is highly appreciated. Thanks...!
You should store dates in SQLite using columns with INTEGER datatype. To store them, use the Date or Calendar classes in Java to convert your date to Unix Format, and then simply order by this column. It's easier.
There is a simplest way if you cannot change your database schema :
there is an id (_id, usually) which is autoincrement. the min(_id) is the oldest row, you can delete it and insert a new row.

Deleting from android sqlite table

I have a column in android sqlite database table. The values in the column are like:
2011/06/01
2011/06/02
2011/06/05
2011/06/10
2011/06/11
2011/06/13
2011/06/15
2011/06/16
2011/06/25
2011/06/26
I have a string today="2011/06/27"
Now I want to delete those rows whose column value is older than 5 days from today.
How to modify the code to achieve this?
return db.delete(DATABASE_TABLE,where date="", null) > 0;
You have to store your dates in a other format, check the date function of SQLite. Use YYYY-MM-DD instead of YYYY/MM/DD. You already got an order on your dates defined by its string representation. So you can use the date function of SQLite to select the correct rows.
Dates older than 5 days would be queried like this.
... WHERE datecolumn < date('now', '-5 days') ...
There is no date type in sqllite
you just store a data represented value as a string or int
then use the date based functions of sqllite to get the values
You should be using the stored procedure rather than writing the service. It will make easier for your application to automatically update the records.
See link: http://searchoracle.techtarget.com/answer/Time-based-stored-procedure-to-check-table-and-update-old-records
Another solution is to use date instead of string. When you run your service then fetch data as Date and then you can make the simple comparison.

How to search a column in an SQLite database for specific text?

More specifically, the first two columns of my read-only database are the row number (the _id integer primary key), and a date and time in the text format 7/21/2011 HH:MM:SS PM. In my Android app, the user selects a start and end time (HH:MM), and then data about that time interval is displayed graphically. How do I search this second row of my database for the inputted times?
So if your Date Column contains TEXT(since there is no DATE in SQLite), you should use the SQLite function
strftime(format, timestring, modifier, modifier, ...)
See docs SQLite Date functions
So you SQL query may look something like this:
SELECT * FROM mytable WHERE strftime('MM-DD-YYYY HH:MM:SS', date_column) < strftime('MM-DD-YYYY HH:MM:SS', '7/21/2010 22:00:00')
where the last date parameter will be the inputted date.
I am not familiar with SQL time commands. If you select all rows and then process them in the java code using Date objects and their .compareTo() methods you can only display rows of interest to your user.

timestamp problem in android sqlite3

I want insert the current date and time into my table....
I used this query
insert into tbl_reminder values("Description",current_timestamp);
but it insert the wrong time...
actually the timestamp in my emulator is 2010-09-16 18:40:06
but the inserted timestamp value is 2010-09-16 13:10:06
what i do to insert the exact time...
Insert it enclosed within Quotes, the date will be stored as a string.
sqlite doesn't has a Date datatype.
Now, the following is just incase you are planning to use the Date column for Comparisions...
Speaking from experience, i would advice you store it as Long Integer, as a Unix Timestamp, it lets you do Comparison between dates, which would otherwise be very difficult.
You'll obviously have to convert it to-and-fro but in the long run it's a better stratergy.

Categories

Resources