How to convert milliseconds to date in SQLite - android

I store date from Calendar.getTimeInMilliseconds() in SQLite DB.
I need to mark first rows by every month in SELECT statement, so I need convert time in milliseconds into any date format using SQLite function only. How can I avoid this?

One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970.
To convert milliseconds to that, just divide by 1000.
Then use some date/time function to get the year and the month:
SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable

Datetime expects epochtime, which is in number of seconds while you are passing in milliseconds. Convert to seconds & apply.
SELECT datetime(1346142933585/1000, 'unixepoch');
Can verify this from this fiddle
http://sqlfiddle.com/#!5/d41d8/223

Do you need to avoid milliseconds to date conversion or function to convert milliseconds to date?
Since sqlite date functions work with seconds, then you can try to
convert milliseconds in your query, like this
select date(milliscolumn/1000,'unixepoch','localtime') from table1
convert millis to seconds before saving it to db, and then use date function in sql query

Related

Query unique date to cursor

In my database dates is stored in miliseconds as string. How to query unique dates considering only month and day and count how many date share same day?
Divide by 1000 to get seconds, then use the unixepoch modifier to get date, and use strftime to output only the year and month:
SELECT strftime('%Y-%m', DateMillis / 1000, 'unixepoch'),
COUNT(*)
FROM MyTable
GROUP BY 1
If you want to store a date, use date type. Sqlite may not have it. But please, do not use it as string! I would suggest you to store integer to store miliseconds sice epoch. Use bindLong() and such. Unfortunately, you have to make some computations yourself in code. Just compute first and last milisecond of each day. Then you would be able to use WHERE timestamp BETWEEN first_day_milisecond AND last_day_milisecond, using even indexes.
You can select min and max of timestamp to try only days between them.
If you need to do this frequently or over a lot of data, I suggest to store date in multiple columns as integers for year, month, day. Depends on what you have to do with time, miliseconds since midnight. Or more columns of hour, minute, second and miliseconds.
I think it is much easier to format string date from bunch of numbers than parsing those numbers from a string. Definitely easier to compare them inside database.

using localtime on a date

I have a column in my SQLite database that stores time values in UTC. How do i get the count of distinct days?
The below gives a result based on the UTC days, which would be wrong in the local timezone :
select distinct(date(column)) from table
The below would consider the time as well, which would be wrong :
select distinct(datetime(column,'localtime')) from table
Would it make sense to convert the date to localtime as below :
select distinct(date(column,'localtime')) from table
I am not sure if using the localtime conversion on a date, as opposed to a datetime, has any effect.
The only difference between date and datetime is in the output format, not in any internal calculations.
(All five date/time functions behave the same in this regard.)

Sqlite get records by hour

I want to make a query to get records between specified hours. For example, i want to get all records between 00:00 and 01:00 for all days. So, the date does not matter but hours. How to do that?
I have done this, but it only return for certain dates.
Select name from my_table where date_column> beginning and date_column< end
Here beginning and end are in millisecond. Also my date_column is stored in millisecond format.
Use strftime():
Select name
from my_table
where strftime('%H', date_column) = '00';
This just checks the hour. You could use '%H:%M:%S' if you wanted more granularity.
EDIT:
You do not have a date time value. You have something else. It looks like a Unix epoch time measured in milliseconds rather than seconds. If so, the following should work:
Select name, datetime(date_column/1000, 'unixepoch')
from my_table
where strftime('%H', datetime(date_column/1000, 'unixepoch')) = '19';
However, none of the times are at hour 3. You may need to convert using your localtime.

Delete where date is 30 days back from now SQL Android [duplicate]

What are the sqlite equivalents of INTERVAL and UTC_TIMESTAMP? For example, imagine you were "porting" the following SQL from MySQL to sqlite:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time > utc_timestamp() - INTERVAL 600 SECOND;
datetime('now') provides you the current date and time in UTC, so is the SQLite equivalent of MySQL's UTC_TIMESTAMP().
It may also be useful to know that given a date and time string, datetime can convert it from localtime into UTC, using datetime('2011-09-25 18:18', 'utc').
You can also use the datetime() function to apply modifiers such as '+1 day', 'start of month', '- 10 years' and many more.
Therefore, your example would look like this in SQLite:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time > datetime('now', '-600 seconds');
You can find more of the modifiers on the SQLite Date and Time Functions page.
There's no native timestamp support in sqlite.
I've used plain old (64-bit) integers as timestamps, representing either micro- or milliseconds since an epoch.
Therefore, assuming milliseconds:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time_millis > ? - 600*1000;
and bind system time in milliseconds to the first param.
there is LOCAL_TIMESTAMP in SQLite, but it's GMT.

Get data from sqlite database per day DATE TIME

I have a sqlite database. I have three columns:_id, date, value.
I now want to extract a count of the _id:s depending on the day in the date, and calculate an average of the int value. This is for an Android app.
So I want to "select the day in date and for each day ( for sixty days), count how many _id:s there are for this day. Finally calculate the average of value.
I guess it is something like :
"SELECT DATE('now' 'days[i]') as date, COUNT(_id) as count, AVG(value) as vl FROM v_efforts WHERE DATE(v_efforts.date) = DATE('now' 'days[i]')";
But I can't get the 'days[i]' to work. I don't know how i can get this value to increase to sixty, and then how I can store the count and vl for each of these sixty days.
THanks a lot!
You'll want to use a GROUP BY expression to aggregate the entries by date. It's not quite clear whether you're looking for the last 60 days of entries in the database, or the entries from the last 60 real days (which would only be the same if you can assume that there are entries every day).
For the former (last 60 days which had database entries), you can use a LIMIT clause:
SELECT date,COUNT(_id),AVG(value) FROM v_efforts GROUP BY date ORDER BY date DESC LIMIT 60;
For the latter (last 60 real days), you can use WHERE:
SELECT date,COUNT(_id),AVG(value) FROM v_efforts WHERE date>DATE('now','-60 days') GROUP BY date ORDER BY date DESC;
The docs for Version 3 are pretty decent:
http://www.sqlite.org/lang_datefunc.html
I would look at the block that deals with the built in date time functions. Since SQLite doesn't support an actual date datetype:
Compute the current date:
SELECT date('now');
Compute the last day of the current month:
SELECT date('now','start of month','+1
month','-1 day');
Compute the date and time given a unix timestamp 1092941466.
SELECT datetime(1092941466,
'unixepoch');
Compute the date and time given a unix timestamp 1092941466, and compensate for your local timezone.
SELECT datetime(1092941466,
'unixepoch', 'localtime');
Compute the current unix timestamp.
SELECT strftime('%s','now');
Compute the number of days since the signing of the US Declaration of Independence.
SELECT julianday('now') -
julianday('1776-07-04');
Compute the number of seconds since a particular moment in 2004:
SELECT strftime('%s','now') -
strftime('%s','2004-01-01 02:34:56');
Compute the date of the first Tuesday in October for the current year.
SELECT date('now','start of year','+9
months','weekday 2');
Compute the time since the unix epoch in seconds (like strftime('%s','now') except includes fractional part):
SELECT (julianday('now') -
2440587.5)*86400.0;

Categories

Resources