sqlite DATETIME formatting and ordering, what am I doing wrong? - sqlite

I have a number of "DATETIME's" for the following form e.x.:
2014-01-15T19:30:00-0800
I am successfully inserting them into an sqlite table that I created with the following statement:
CREATE TABLE STUFF(id unique,date_time DATETIME)
When I query using the statement below I get all the dates I inserted back but not ordered.
SELECT * FROM STUFF ORDER BY DATETIME(date_time) DESC;
I'm guessing this is a formatting issue but I'm not sure. Can anyone spot what I'm doing wrong?

This is not a date format supported by SQLite;
a time zone indicator must have the form ±HH:MM:
> SELECT DATETIME('2014-01-15T19:30:00-0800');
> SELECT DATETIME('2014-01-15T19:30:00-08:00');
2014-01-16 03:30:00

Related

Get mariadb to handle date searches like mysql (e.g. with date like 2020-12-32)

With mysql it is possible to search for a full month with a sql query like
select * from table where date_created >= '2020-12-01' and date_created <= '2020-12-32'
column date_created is datetime type.
This does not work with mariadb. I know that the date 2020-12-32 is invalid. But is it possible to get this working with mariadb?
There is no way to make this statement work without modifying either your code or your table definition:
Since the date_created is defined as date type, the strings in the where clause will be converted to date before doing comparison. The date "2020-12-32" is invalid and will be converted to "0000-00-00" which means the where condition becomes WHERE date_created > 2020-12-01 and date_created < 0000-00-00: therefore the statement will not return any rows.
However it is interesting that the optimizer doesn't figure out an impossible WHERE condition and is doing a scan.

Error during compare date format in sqlite query

I was writing an application on c#, which is save DateTime to the Text field in the database.
Format of date is:
'1/15/2020 14:48:44', '2/11/2020 12:53:59' and etc
Now I want to get records with SQL query by the last few days.
I understand that I need convert text to date, and compare. But SQL query return 0 results. My query is:
select * from LogRootProcessing where strftime(dateparsing) < strftime('%MM/%DD/%YYYY %HH:%MM:%SS','2/11/2020 12:53:59')
Could you please advise how to fix the query?
Thanks in advance.

How the where clause will work for the date field in the teradata

I would like to apply a where clause on the date field in the teredata, but it shows empty records. Below is the query used to get the data.
select * from table name where date = '2019-05-01'
Kindly suggest the correct format.
Try the below format:
select * from table x where date = DATE '2019-05-01'
Check this site

Convert integer column to date column using sqlite query

I am using SQLite.
In my database, I have a integer column value like 20050819. I would like to convert this as date column as like 2005-08-19.
I can achieve my requirement when using the function CONVERT(columnName, DATETIME) in MySQL server. But I need the same result in SQLite.
Is this possible in SQLite query?
You basically have to break apart the date:
select printf('%4d-%02.2d-%02.2d',
columnname/10000, columnname%10000/100, columnname%100)
from tablename;
P.S.
If you have the choice, you will be far better off storing the date in ISO standard format ('YYYY-MM-DD') in the database in the first place.

Validate Date Time value on Sqlite before insert / update

as we all know that the correct format for date and time value in sqlite is :
2014-06-18 01:00:00
my question : is there any method to validate date and time value before insert / update on sqlite, maybe when creating table, to make sure date and time data is correct, thanks
To enforce that a value is in any of the supported formats, you could just check that some built-in date function is able to parse it:
CREATE TABLE MyTable(
MyDate CHECK (date(MyDate) IS NOT NULL),
[...]
);

Resources