I have an sqlite database which currently holds an integer field called Year which currently only stores the year. In future versions I want to store a full date and time.
I updated my table to include a FullDate field using alter table.
> ALTER TABLE Files ADD COLUMN UploadDate DATETIME DEFAULT 0;
Next, I want to migrate all the existing year fields to the new field. So I'm looking for something like:
> UPDATE Files SET UploadDate = (DATETIME('%Y-%m-%d', Year, 1, 1));
Unfortunately this doesn't seem to work as the result is empty. I also tried the date and strftime functions but they either result in incorrect data or empty data.
What's the proper way to update a DATETIME field with existing data in the same table?
The DATE and DATETIME functions don't have a format parameter.
For more: http://sqlite.org/lang_datefunc.html
The main catch is that SQLite does not have any date or time types, so that you might as well populate your field with:
UPDATE Files SET UploadDate = Year || '-01-01';
And that will do the exact same thing. Dates are not stored as typed, but can be evaluated as such against the date and time functions.
Related
In Tableau Prep I have an output table that I want to import into an Oracle database.
In that output table, there's a column (file_date) with a date value (ex.: '2021-01-01'). The date value is the same for all rows.
Output table:
enter image description here
I need to write a custom SQL query (in Tableau Prep) that checks if my Oracle table already has any rows where the date = '2021-01-01'. If so, all rows need to be deleted before I import the new data.
Table_1:
enter image description here
Something like:
DELETE FROM table_1 WHERE date_column = '2021-01-01';
After checking it will find that the 1st row has the date = '2021-01-01' and deletes that row.
Table_1 after:
enter image description here
As the date changes every time new files come up, manually entering the date in the query is not a possibility. Is there any way to use a value from my table in a custom SQL query?
I'm aware that Tableau Desktop allows for the creation of parameters, but that's not available in Tableau Prep.
If the output is all the same date, then that must also be true of the input data. There is no reason to check for existing data values. Since finding no matching records on delete does not throw an error, checking is not necessary - just delete.
delete
from table_1
where date_column =
(select date_column
from import_table_1
fetch first 1 row only
);
In Sqlite I want to extract the date and time portions of a DateTime field separately in a view and return them also as a datetime, not strings. I've tried Cast, Date(), datetime(), but they all return strings.
I've read the SQLite documentation and understand how there is not an actual Date data type. Yet a Table field defined as DateTime is able to be parsed as a Date by an Excel query, but calculations on that field are not. I'm trying to do all data prep in the database view.
My data has the following field taken directly from the table definition:
LastModifiedDate datetime
I want the date (without time) to have the same DateTime data type as LastModifiedDate, not Text, because I use this view in many spreadsheets. I can apply Excel Date functions and formatting to LastModifiedDate field directly as returned from the ODBC query to Excel, and want to do the same to the Date-only part. I don't want to have to put a string-to-date conversion in every spreadsheet when I know it can get the date natively from Sqlite in LastModifiedDate.
SELECT LastModifiedDate,
date(LastModifiedDate) as Datepart,
cast(LastModifiedDate as numeric) as Date2
FROM Transactions
LastModifiedDate Datepart Date2
2019-07-28 18:22:38.9165394 2019-07-28 2019
LastModifiedDate in the above query is interpreted in Excel as a date to which date formats and date functions can be applied with no further processing required. Datepart above is returned as Text to Excel, and I can't apply date functions and formats without further pre-processing in Excel. I would like Datepart to be interpreted a date in Excel just as LastModifiedDate is.
I'm looking at the ch-werner.de sqliteodbc-0.9998. It will return an ODBC TIMESTAMP type only if the column decltype starts with timestamp or datetime. It returns ODBC TIME only for decltypes starting with time and ODBC DATE only for decltypes starting with date.
sqlite3 provides this decltype only for result table columns that are direct database column references. So if your SELECT statement has some expression that is more than a plain column reference, the decltype is lost. sqlite3 works like this at least up to version 3.39.0. It is documented.
The CAST expression converts the value of given expression to a storage classes by the determined affinity of the given declared type, but does not assign decltype to the result.
If you want to see the decltypes for query columns, you can use the sqlite3 cli and give it command .stats 2. Then it'll output the column declared types for each statement it executes.
If the decltype is found, the sqliteodbc-0.9998 will always parse string values into ODBC types. If DSN Option JDConv is enabled, it'll also parse floating point julianday values (whether provided as float or a string of a float) into ODBC types and when writing it'll write floating point into database.
If you can afford to change the schema, you can add a generated virtual column. This is cheap in storage, because data is not affected, but it costs when you query the column. This column can calculate other column into the values and decltypes you need for ODBC.
ALTER TABLE data ADD COLUMN
Datepart date AS (date(LastModifiedDate))
Then to get the Datepart, you simply query the column.
SELECT Datepart FROM data
create table abc_test(
id number,
rv timestamp
);
insert into abc_test(id,rv) values (1, CURRENT_TIMESTAMP);
Is there any better way to do it?
Can the timestamp be stored in the database in some binary format like "01x0000000000046565".
In Microsoft sql, this can easily be done as below :
create table xyz_test(id1 timestamp, name varchar(50))
insert into xyz_test (Name) values('person1')
select * from xyz_test
The above query will show some binary form for the timestamp column and "person1" in the name column
How to get the same result in the oracle?
It is indeed stored in a binary format -- the tool that you are using translates it when it is retrieved.
Display of dates and timestamps is dependent on tool configuration, and on session settings etc.. You can read the timestamp in whatever format you like -- consult the documentation for To_Char(datetime).
i want to get all entries from a SQLite table, which have the timestamp from the same month.
For example, the user can type in "July" and then i want to get all entries made in the 7. month.
The current "time"-column is a simple string and in the Format (DD.MM.YYYY HH:MM:SS)
Is there a way to do this with SQLite or will i need to use code in my program?
Assuming that your time strings have a fixed length, you could use a query like this:
SELECT * FROM MyTable WHERE time LIKE '__.07%';
However, you should always stored dates in one of the supported date/time formats so that you are able to use the built-int date/time functions.
i want to create a table on sqlite with one field as DateTime (YYYY-MM-DD) , how i can create it?
i'm trying with:
create table test (_date datetime);
but i'm not sure if the datatype is correct 'cause i can do this:
create table test (_date nyanType);
and no error occours
SQLite is rather unique in that its columns are not statically typed. You can technically store a string in a column that was created as an integer column.
If you check out the SQlite Documentation for types, you'll see that SQLite dosn't have a date type, but it exposes date and time functions that are suitable for manipulating dates that are stored as TEXT, REAL or INTEGER. You should use those instead.