I have this query which works in SQL server:
Convert(datetime, [ChangedDate]) >= DATEADD(DAY, -3, CAST(GETDATE() AS DATE))
I want to make it work in Android SQLite database.
As far as I understood I need to use something like: date('now','+14 day') instead of DATEADD, but it gives me an error on datetime (it could be here Convert(datetime,...) in sqlite.
Can you modify this query in order to make it works on SQLite?
SQLite does not have a date data type. So you're not required to use convert or cast. A query like this would work:
select *
from table1
where col1 < datetime('now', '-3 days')
Example at SQL Fiddle.
For more details, see the SQLite manual:
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian
day numbers, the number of days since noon in Greenwich on November
24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER
as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
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
I have dates stored in my Database in a dd/mm/yyyy format and I am looking to only show items where the date is equal to or great than today.
I have tried WHERE date(fixtures.date) >= date('now') but I got no results.
Below is my query, any help would be greatly appreciated
SELECT fixtures.id,
fixtures.team_1_id,
fixtures.team_2_id,
fixtures.date,
fixtures.time,
fixtures.pitch,
teams.team_name,
teams_1.team_name AS awayTeam,
leagues.league_name
FROM fixtures
INNER JOIN teams
ON fixtures.team_1_id = teams.id
INNER JOIN teams AS teams_1
ON fixtures.team_2_id = teams_1.id
INNER JOIN teams_vs_leagues
ON teams.id = teams_vs_leagues.team_id
INNER JOIN leagues
ON teams_vs_leagues.league_id = leagues.id
WHERE date(fixtures.date) >= date('now')
ORDER BY fixtures.date DESC
Why do you say your dates are store in dd/mm/yyyy format? According to SQLite docs
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
Basically this query won't work
select 1 where '01-08-2016' >= date('now')
but this will
select 1 where '2016-08-01'>= date('now')
So can you verify if your date fixtures.date is format as 'yyyy-MM-dd' otherwise your query won't work. Additionally remember to use date('now', 'localtime') to get your local time.
If your date is well formated you can try to do something like
SELECT fixtures.id
FROM fixtures
WHERE fixtures.date >= date('now')
if you do get results with this, then the joins are not matching any row.
For further information you can check this answer
I have a SQlite database with date stored as string datatype (dd-mm-yyyy format). How can I sort the rows in my database by date ?
The Query for sorting date is:
SELECT EventDate,Event,ID from EventCalenderTable Order By EventDate ASC
Note:
SQLite only knows three date formats:
Text ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
Real Julian day numbers since November 24, 4714 B.C
Integer number of seconds since 1970-01-01 00:00:00 UTC
SQLite does have five date/time functions for converting between formats.
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.)
I'm attempting to store 3 values in a database I've created on an android device. The values are NAME, DATE, and TIME. My question is thus: what is the best approach to fetch and store the current DATE and TIME values when they are inserted into the database?
So far I have identified two ways to do this, either by making system calls in android like //values.put(TIME, System.currentTimeMillis()); or using SQL functions like GETDATE(). But, which is better?
If I'm heading off in the wrong direction, please let me know and direct me towards the right direction.
Using the SQL functions will be more compatible with other tools using the same SQL engine.
Using text in ISO format YYYY-MM-DD hh:mm:ss.fff will be widely compatible and still sort properly.
The sqlite docs give these column type options for storing dates:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
You could use TEXT as described by Doug Currie, or you could use INTEGER using a java.util.Date. (I don't know why you'd use the REAL option.)
To use INTEGER, you could load and store dates something like this:
// build a date from a Cursor
Date d = new Date(cursor.getLong(DATE_FIELD_INDEX));
// add a date to an SQLiteDatabase
final ContentValues values = new ContentValues();
values.put(DATE_FIELD, d.getTime());
db.insert(TABLE_NAME, null, values);
This will be less readable in your database, but comparisons between dates (in your query) should be faster since comparing numbers is faster than strings. (Although I don't know much about the sqlite implementation.)