Sqlite query to convert string to date in sqlite? - android

I have date in string format in the table in yyyy-mm-dd format.
I want to convert that string to date format for further comparision on the date.
Any help would be greatful..

I'd suggest you to use SQLite - DateTime functions to compare field data value. Alternately you can use java.text.SimpleDateFormat class's parse method to convert string date to Date type.

I got the query of this.
SqlLite compares difference between two string date directly.
select column from Table where columnDate between '2012-07-01' and '2012-07-07'
But thanks for your interest guys.

Related

Realm Query which filters dates with datatype String

I have a Realm Object and I want to create a Query with a filter. My Realm Object has the attribute date. It has the format dd/mm/yyyy and is a String.
I want to create a Query that searches all entries where the date is a date in the future.My problem is that the function greaterThanOrEquals doesn't take a String (such as Date) as a parameter.
The result that I'm expecting is the following: Today is 09/07/2017. If an Element has the date of today or some date in future (e.g 10/07/2017), it should be in the result list. If it is in the past, then it should not be in the list.
Any help would be very much appreciated

Convert column dateformat inline with sql query

I have stored date in a column of android sqlite database in YYYY/MM/DD format . Now I want to use julianday function in sql query to select special rows of database however julianday uses date in YYYY-MM-DD format . How can I convert date format inline with sql query?
Please rewrite this sample query which "startdate" and "enddate" are stored in YYYY/MM/DD format not YYYY-MM-DD:
select * from events where julianday(enddate)-julianday(startdate)>1200
SQL query accepts inline replace:
select * from events where julianday(replace(enddate,'/','-'))-julianday(replace(startdate,'/','-'))>1200
for more complex conversion use prepared SQL convert() function

Datetime aritmethics in sqlite on android

I am trying to do some datetime aritmethics in Sqllite. I have a table with two rows defined as type DATETIME. These fields are called LOG_FROM and LOG_TO. In a query on this table I try to do the following:
SELECT SUM(LOG_FROM - LOG_TO) AS TIMESPENT FROM LOG_TABLE WHERE X=Y;
This subtraction inside the SUM function does not work as intended. It will always return the number 0. I read the data via a cursor.getString.
When I store dates in the table I convert them to strings on the format yyyy-MM-dd HH:mm:ss
SQLite ignores column data types. Your date strings are strings.
Your date format is supported by the built-in date functions, so you can use them to convert the dates to numbers.
Assuming that you want the result to be a number of days, use julianday:
SELECT SUM(julianday(LOG_TO) - julianday(LOG_FROM)) ...

Save SQLite TIMESTAMP in the format of MySQL TIMESTAMP, Android

I want to save current TIMESTAMP to a SQLite database in Android.
But it should be in MySQL TIMESTAMP format which is 2014-04-02 20:04:05
How to make it in db.execSQL();
Please help!
Have a look at SQLite date and time functions. You would do something like
strftime(%Y-%m-%d %H:%M:%S, 'now') in your SQL string. This particular format also has a shortcut: datetime('now')
db.execSql("insert into table (column) values ( datetime('now') )");
My preference though is to simply store the current time as a long, which you can then format any way you like (or any way the user likes, or any way the system defaults to showing dates/times).
You should use the following date format in order to insert dates/times in SQLite:
DateFormat dateFormatISO8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
and retrieve in string as below:
String crntDate = dateFormatISO8601.format(new Date());
P.S. Make sure your column is of type DATETIME

Issues in storing and retrieving data from sqlite in android

I am trying to retrieve data from the database with the following query:
select * from Oil Where date(DDate)=date('2012-08-07');
where DDate is one of the column in the Oil table with datatype text.I get the Arrayindexoutofboundsexception. I found that it is not returning any value the count is 0.
can anyone help me with this.What i am actually trying is to do is store date value in the table.I couldn't find which datatype should be used to save date,from the sqlite site is that it doesn't have a particular datatype for date and time.
SQL Server comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
select * from Oil Where date(DDate)=date('2012-08-07');
can be
select * from Oil Where DDate='2012-08-07';
If you have DDate in the date format 'yyyy-MM-dd' then you can directly compare the date using
select * from Oil Where DDate='2012-08-07';
else if you have DDate in the date format 'yyyy-MM-dd HH:mm:ss' then the query looks like this
select * from Oil Where date(DDate)='2012-08-07';
If you are saving date in sqlite database and you have to perform date operations like sorting or using date() and time() functions then you have to use the 'yyyy-MM-dd HH:mm:ss' date format.
Datatype doesn't matter for sqlite.

Categories

Resources