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)) ...
Related
I have a table with timestamp column and the values stored in timestamp column are like
20180608T002304.507Z , 20180608T001745.821Z, 20180608T001628.170Z, 20180608T001336.516Z
I would like to get timestamp in "YYYY-MM-dd" formate.
Used strftime() function , but no use
when I query strftime('%Y-%m-%d %H:%M', timestamp) getting null
Thanks in advance
This is not one of the supported time string formats. Change the values so that they contain the appropriate punctuation:
sqlite> SELECT date('20180608T002304.507Z');
sqlite> SELECT date('2018-06-08T00:23:04.507Z');
2018-06-08
Your issue is that strftime along with all the SQLite date functions require specific formats as listed below. 20180608T002304.507Z is not one of the formats, hence the null.
Note the following is based upon your query using strftime('%Y-%m-%d %H:%M', timestamp) as opposed to I would like to get timestamp in "YYYY-MM-dd" formate.
You have two options.
1. You could utilise the substr function e.g.
:-
substr(mytimestamp,1,4)||'-'||
substr(mytimestamp,5,2)||'-'||
substr(mytimestamp,7,2)||' ' ||
substr(mytimestamp,10,2)||':'||
substr(mytimestamp,12,2)
where mytimestamp is the column name
As an example, the following :-
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (mytimestamp);
INSERT INTO mytable VALUES('20180608T002304.507Z'),('20180608T001745.821Z'),('20180608T001628.170Z'),('20180608T001336.516Z');
SELECT
substr(mytimestamp,1,4)||'-'||
substr(mytimestamp,5,2)||'-'||
substr(mytimestamp,7,2)||' ' ||
substr(mytimestamp,10,2)||':'||
substr(mytimestamp,12,2)
FROM mytable;
results in :-
2. Alter the source data to match one of the acceptable/recognised formats.
This could be done using something based upon :-
UPDATE mytable SET mytimestamp =
substr(mytimestamp,1,4)||'-'|| -- Year
substr(mytimestamp,5,2)||'-'|| -- Month
substr(mytimestamp,7,2)|| -- Day
substr(mytimestamp,9,1)|| -- T (or space)
substr(mytimestamp,10,2)||':'||
substr(mytimestamp,12,2)||':'||
substr(mytimestamp,14)
;
This based upon the table that was created above.
After running the update then using :-
SELECT strftime('%Y-%m-%d %H:%M', mytimestamp) FROM mytable;
results in :-
Time Strings A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
In formats 5 through 7, the "T" is a literal character separating the
date and the time, as required by ISO-8601. Formats 8 through 10 that
specify only a time assume a date of 2000-01-01. Format 11, the string
'now', is converted into the current date and time as obtained from
the xCurrentTime method of the sqlite3_vfs object in use. The 'now'
argument to date and time functions always returns exactly the same
value for multiple invocations within the same sqlite3_step() call.
Universal Coordinated Time (UTC) is used. Format 12 is the Julian day
number expressed as a floating point value.
Formats 2 through 10 may be optionally followed by a timezone
indicator of the form "[+-]HH:MM" or just "Z". The date and time
functions use UTC or "zulu" time internally, and so the "Z" suffix is
a no-op. Any non-zero "HH:MM" suffix is subtracted from the indicated
date and time in order to compute zulu time. For example, all of the
following time strings are equivalent:
2013-10-07 08:23:19.120
2013-10-07T08:23:19.120Z
2013-10-07 04:23:19.120-04:00
2456572.84952685
In formats 4, 7, and 10, the fractional seconds value SS.SSS can have
one or more digits following the decimal point. Exactly three digits
are shown in the examples because only the first three digits are
significant to the result, but the input string can have fewer or more
than three digits and the date/time functions will still operate
correctly. Similarly, format 12 is shown with 10 significant digits,
but the date/time functions will really accept as many or as few
digits as are necessary to represent the Julian day number.
SQL As Understood By SQLite - Date And Time Functions
So I'm trying to query all the same dates but this code won't work
SELECT * FROM schedTBL WHERE CONVERT(varchar, DueDateTime, 120) LIKE " + "'%"+ text + "%'"
it returns no such column:varchar
This looks like a SQL statement for SQL Server, not SQLite...
Assuming you are storing dates in field DueDateTime without the hour part, something like this should do:
SELECT * FROM schedTBL WHERE DueDateTime = '2016-09-24'
Or you'll need like if you also have hour values. That depends on how you are actually storing dates/times in your SQLite DB.
Try formatting the date in Java and send it as a parameter to your query.
SQLite doesn't handle dates like SQL Server.
See:
SQLite Date And Time Functions
In SQLite, date values would be formatted with the strftime() function, or for this particular format, with the date() function:
... WHERE date(DueDateTime) LIKE ...
But this works only for supported date formats.
If the values in your database use an unsupported format, your only choice is to convert them, or to try to match the actual string:
... WHERE DueDateTime LIKE ...
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.
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.
I am using SQLite database for my application.
The Table Structure Goes Like :
_id : integer primary key
name : text
day : date
I am able to store date in format : dd-mmmmm-yyyy eg. 15-June-2011
But when i tried to retrieve all records filtered by date from the database it returns me null.
database.query(DATABASE_TABLE, new String[] { "strftime('%d-%mm-%Y',date('now'))","strftime('%d-%m-%Y',"+KEY_DAY+")" },
"strftime('%d-%m-%Y',date('now'))=" + KEY_DAY , null,null,null,null,null);
It didnt match with anyrow's date even though there were some matching dates.
I have already gone thru documentation of SQLite. But didn find any solution yet.
I want to have something like :
select * from table where day=curdate();
How can i do the same task in SQLite ?.
(Yes I am flexible to change the format of date stored in Dateabase)
What are other alternatives for the same task ?.
In java programming you can convert any date format into long (time in milliseconds) and viceversa. My opinion is while storing format the date into long format in java and then store long value of date in database. also while retrieving you can retrieve the long value and then format that as per your expected date format. I have been using this type of logic for several application.
Thanks
Deepak.
The function strftime('%d-%m-%Y',date('now')) returns a string with the month in numeric format (from 01 to 12). As far as I can tell from the docs, there is no format specifier to return the full name of the month.
I think you'll have to store your dates using numerical month specifiers instead of names.