I'm trying to find all the results between two dates in SQLite.
select log_ID
from Message
where Timestamp between DATE('2014-04-17 03:27:08','YYYY-MM-DD HH:MI:SS')
and DATE('2014-04-18 03:27:08','YYYY-MM-DD HH:MI:SS');
This query is executing successfully but it gives no results.
I've tried using DATE and TO_DATE functions as well.
Schema:
CREATE TABLE Message(
Log_ID INTEGER PRIMARY KEY,
Session_ID TEXT NOT NULL,
Timestamp DATETIME NOT NULL,
Admill_Msg TEXT, Logcat_Msg TEXT);
Sample tuples:
155|admil.out.txt|2014-04-17 03:26:48.730000||PID:926 TID:926 TAG:I/Zygote LOG:Preloading resources...
156|admil.out.txt|2014-04-17 03:26:48.730000||PID:926 TID:926 TAG:W/Resources LOG:Preloaded drawable resource #0x1080096 (android:drawable/toast_frame) that varies with configuration!!
157|admil.out.txt|2014-04-17 03:26:48.740000||PID:926 TID:926 TAG:W/Resources LOG:Preloaded drawable resource #0x1080105 (android:drawable/btn_check_on_pressed_holo_light) that varies with configuration!!
You are using the date function wrong; there is not format parameter.
strftime could do such formatting, but that is not necessary because the timestamps are simply strings.
Just compare the strings directly:
select log_ID
from Message
where Timestamp between '2014-04-17 03:27:08'
and '2014-04-18 03:27:08';
Related
I constantly retrieve JSON data from some API and put that data into a MariaDB table.
The JSON ships with a timestamp which I'd like to place an index on, because this attribute is used for querying the table.
The JSON looks something like this (stripped):
{
"time": "2021-12-26T14:00:00.007294Z",
"some_measure": "0.10031"
}
I create a table:
CREATE TABLE some_table (
my_json JSON NOT NULL,
time TIMESTAMP AS (JSON_VALUE(my_json , '$.time')),
some_measure DOUBLE AS (JSON_VALUE(my_json , '$.some_measure'))
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_general_ci;
my_json holds the entire JSON snippet, time and some_measure are virtual columns properly extracting the corresponding JSON values on the fly.
Now, trying to add an index on the TIMESTAMP attribute:
CREATE INDEX some_index ON some_table (time);
This fails:
SQL Error [1292] [22007]: (conn=454) Incorrect datetime value:
'2021-12-26T14:00:00.007294Z' for column `some_db`.`some_table`.`time` at row 1
How can I add an index on that timestamp?
The issue here is that converting a string (the JSON timestamp) to a TIMESTAMP is non-deterministic because it involves server side settings (sql_mode) and timezone settings.
Indexing virtual columns which are non-deterministic is not supported.
You would want to use a VARCHAR data type instead and index that:
CREATE TABLE some_table (
my_json JSON NOT NULL,
time VARCHAR(100) AS (JSON_VALUE(my_json , '$.time')),
some_measure DOUBLE AS (JSON_VALUE(my_json , '$.some_measure'))
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_general_ci;
You should be able to create your index:
CREATE INDEX some_index ON some_table (`time`);
You can still query time because MariaDB automatically converts DATETIMEs if used against a VARCHAR:
SELECT
*
FROM some_table
WHERE time > '2008-12-31 23:59:59' + INTERVAL 1 SECOND;
The query will use the index:
I finally came up with a solution that works for me.
Changes are:
use STR_TO_DATE() to create a valid DATETIME from the JSON timestamp
make the generated (virtual) column PERSISTENT
use data type DATETIME instead of TIMESTAMP
So the new code looks like this:
CREATE TABLE some_table (
my_json JSON NOT NULL,
time DATETIME AS (STR_TO_DATE((JSON_VALUE(my_json , '$.time')), '%Y-%m-%d%#%T%.%#%#')) PERSISTENT,
some_measure DOUBLE AS (JSON_VALUE(my_json , '$.some_measure'))
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_general_ci;
CREATE INDEX some_index ON some_table (`time`);
I have following pages in my LibraryApp, developing using Ionic 3 framework:
CategoryPage : To add new record in Category table
BookPage : To add new record in Book table
Transaction: To add daily transaction where TrxType is either "BookIssued" or "BookReturned"
CategorySummaryPage: summary for each CategoryID as on specific day
StudentSummaryPage: summary for each StudentID as on specific day
and following tables in my SQLite database :
Category(ID INTEGER,Name TEXT)
Book(ID INTEGER,CategoryID INTEGER,Title TEXT)
Transaction(TrxID INTEGER, TrxDate TEXT,TrxType TEXT,BookID INTEGER,DueReturnDate TEXT)
Student(StudentID INTEGER,StudentName TEXT)
CategorySummary(SummaryDate TEXT,CategoryID INTEGER,TotalBooksIssued INTEGER,TotalBooksReturned INTEGER)
StudentSummary(SummaryDate TEXT,StudentID INTEGER,TotalBooksIssued INTEGER,TotalBooksReturned INTEGER)
While saving new Transaction record, I am getting TrxDate from User(by default,its today date) and then adding 15days in it to get DueReturnDate
i.e. DueReturnDate=TrxDate+15days.
TypeScript variables to store TrxDate and DueReturnDate are of type "Date"
How to ensure that values stored in above TypeScript variables and database columns are in format "YYYY:MM:DD HH:mm:ss"
so that when I select data from Transaction table I can do comparison and/or sorting on TrxDate and DueReturnDate
It is possible to store the date as YYYY:MM:DD HH:mm:ss format to do this try the below code which will convert the date to YYYY:MM:DD HH:mm:ss format.
this.save_details.date = new Date().toISOString();
I have the following query working in pure SQL on SQLite but do not know how to convert it to pyDAL:
SELECT * FROM buy WHERE date('now','-2 days') < timestamp;
The buy table schema is:
CREATE TABLE "buy"(
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"order_id" CHAR(512),
"market" CHAR(512),
"purchase_price" DOUBLE,
"selling_price" DOUBLE,
"amount" DOUBLE
, "timestamp" TIMESTAMP, "config_file" CHAR(512));
Instead of using the SQLite date function, you can create the comparison date in Python:
import datetime
cutoff_time = datetime.datetime.now() - datetime.timedelta(2)
rows = db(db.buy.timestamp > cutoff_time).select()
Alternatively, you can also pass a raw SQL string as the query:
rows = db('buy.timestamp > date("now", "-2 days")').select(db.buy.ALL)
Note, in this case, because the query within db() is simply a string, the DAL will not know which table is being selected, so it is necessary to explicitly specify the fields in the .select() (alternatively, you could add a dummy query that selects all records, such as (db.buy.id != None)).
Context is Teradata SQL Assistant
Successfully created the following table:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
1) The following INSERT works successfully:
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CURRENT_DATE);
2) The following INSERT does not work and returns with Error:INSERT Failed [26665] Invalid date.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (02/02/2016);
3) However, if I use a string 'date value' and CAST it as a Date it works.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') );
I need to know how to make example #2 work? Please Advise?
02/02/2016 is an INTEGER calculation, dividing 2 by 2 by 2016, results in zero, of course this is not a valid date.
CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') works because it tells the parser how to convert the string to a DATE.
The only recommended (and the shortest) way is a Standard SQL DATE literal:
DATE '2016-02-03'
You never need to think about formats because there's only one: YYYY-MM-DD
Actually, this format works too
insert into table_name (datecol) select '2015/12/31';
In your example:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
--EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
EXPLICIT_DATE DATE
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) select '2016/02/02';
When defining a relation, I want to update an attribute to the timestamp at insert. For example, a working table that I have right now
CREATE TABLE t1(
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
txt TEXT);
This is updating a timestamp on insert, for example, insert into t1 (txt) values ('hello') adds the row 1|2012-07-19 08:07:20|hello|. However, I want to have this date formatted in unixepoch format.
I read the docs but this wasn't clear. For example, I modified the table relation to time TIMESTAMP DEFAULT DATETIME('now','unixepoch') but I get an error. Here, as in the docs, now was my time string and unixepoch was the modifier but it didn't work. Could someone help me how to format it as a unixepoch timestamp?
Use strftime:
sqlite> select strftime('%s', 'now');
1342685993
Use it in CREATE TABLE like this:
sqlite> create table t1 (
...> id integer primary key,
...> time timestamp default (strftime('%s', 'now')),
...> txt text);
sqlite> insert into t1 (txt) values ('foo');
sqlite> insert into t1 (txt) values ('bar');
sqlite> insert into t1 (txt) values ('baz');
sqlite> select * from t1;
1|1342686319|foo
2|1342686321|bar
3|1342686323|baz
See https://www.sqlite.org/lang_createtable.html#tablecoldef
If the default value of a column is an expression in parentheses, then the expression is evaluated once for each row inserted and the results used in the new row.
Note 'timestamp' is not a data type known to SQLite (see list here). The default value generated by strftime() would actually be stored as Text.
If it is important to store the value as a number instead of as a string, declare the field as an Integer and add a CAST() into the mix, like so:
create table t1(
...
ts_field integer(4) default (cast(strftime('%s','now') as int)),
...
);
Indeed strftime, which can also be used like so:
SELECT strftime('%s', timestamp) as timestamp FROM ... ;
Gives you:
1454521888
'timestamp' table column can be a text field even, using the current_timestamp as DEFAULT.
Without strftime:
SELECT timestamp FROM ... ;
Gives you:
2016-02-03 17:51:28