I'm trying to write an sqlite statement which returns a date from the table with a certain number of days added from another column. The date is stored as YYYY-MM-DD HH:mm:ss
and the number of days just as an integer.
I've got
SELECT strftime('%Y-%m-%d %H:%M:%S' ,
strftime('%s',transactions.date)+repeattransactions.interval*24*60*60)
FROM transactions,repeattransactions
but this is adding wierd amounts to the years and all sorts. Wonder if I could get some help adding the days and outputting it in the same format?
Thanks.
Use the STRFTIME() function to format date\time\datetime data in SQLite. This function takes two arguments. The first argument is a format string containing the date/time part pattern. In our example, we use the format string '%d/%m/%Y, %H:%M'.
The date() function returns the date as text in this format: YYYY-MM-DD. The time() function returns the time as text in this format: HH:MM:SS. The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.
SQLite TIME() extracts the time part of a time or datetime expression as string format. The time() function returns the time as HH:MM:SS.
To calculate the difference between the timestamps in SQLite, use the JULIANDAY() function for both timestamps, then subtract one from the other. This way, you get the difference in days. The integer part of the difference is the number of full days, and the decimal part represents a partial day.
$ sqlite3
SQLite version 3.7.7 2011-06-23 19:49:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table t (tdate, tinterval);
sqlite> insert into t values ('2011-09-08 11:11:11', 5);
sqlite> select datetime(t.tdate,'+'||t.tinterval||' days') from t;
2011-09-13 11:11:11
See SQLite3 Date & Time Functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With