Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a SQL server query to equivalent in SQLite

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?

like image 273
Ali Avatar asked Apr 28 '26 23:04

Ali


1 Answers

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.

like image 57
Andomar Avatar answered May 01 '26 13:05

Andomar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!