Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a sql server query to sqlite

I'm converting a Sql Server query to sqlite and I am in the process of learning the SQLite queries. Regardless this is the one area that I am having a hard time with and that is the dates.

DATEADD(dd, 0, DATEDIFF(dd, 0, tblSomeTable.EventDate)) >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

What would the equivalent to this in sqlite be? Thank you in advance.

The Event date field is a accepted datetime format - "YYYY-MM-DD HH:MM:SS.SSS"

like image 457
yams Avatar asked Jan 07 '16 00:01

yams


People also ask

Is SQLite compatible with SQL Server?

While SQLite is a light-weight database meant to be embedded into end programs. SQLite is often useful even to SQL Server professionals when they need to create smaller databases without a full server setup to support. It can also be useful for rapid prototyping before moving to SQL Server.

Is SQLite syntax the same as SQL?

The most basic difference between SQLite and SQL is : SQL is a query language which is used by different SQL databases. It is not a database itself. SQLite is a database management system itself which uses SQL.

Which is better SQLite or SQL Server?

SQLite is generally a lot faster than SQL Server. However, SQLite only supports a single writer at a time (meaning the execution of an individual transaction). SQLite locks the entire database when it needs a lock (either read or write) and only one writer can hold a write lock at a time.


1 Answers

DateTime is stored as TEXT in SQLite, so you can just compare strings:

CREATE TABLE tblSomeTable (EventDate TEXT);
INSERT INTO tblSomeTable (EventDate) VALUES
    (strftime('%Y-%m-%d %H:%M:%f', 'now', '-1 day')),
    (strftime('%Y-%m-%d %H:%M:%f', 'now'          )),
    (strftime('%Y-%m-%d %H:%M:%f', 'now', '+1 day'));

SELECT EventDate,
       substr(EventDate, 1, 10),
       strftime('%Y-%m-%d', 'now'),
       substr(EventDate, 1, 10) >= strftime('%Y-%m-%d', 'now')
FROM tblSomeTable;

SELECT EventDate,
       substr(EventDate, 1, 10),
       date(),
       substr(EventDate, 1, 10) >= date()
FROM tblSomeTable;

The result in both cases (at the date of this edit :-) would be:

2016-01-14 12:34:56.789|2016-01-14|2016-01-15|0
2016-01-15 12:34:56.789|2016-01-15|2016-01-15|1
2016-01-16 12:34:56.789|2016-01-16|2016-01-15|1
like image 88
Y.B. Avatar answered Sep 30 '22 07:09

Y.B.