Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite adding days to a date

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.

like image 469
Josh Avatar asked Sep 08 '11 15:09

Josh


People also ask

How do I change the date format in SQLite?

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'.

Does SQLite have date format?

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.

What is the use of SQLite time () function?

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.

How does SQLite calculate date difference?

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.


1 Answers

$ 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

like image 148
Doug Currie Avatar answered Oct 12 '22 23:10

Doug Currie