SQLite Forum

Select Last 24 hours
Login
> I tried datetime('now', '-24 hour') and it gives a syntax error. 

That's improbable.  Next time please give the full statements you use, especially since you have self-admitted to be new to it, it really helps us see where a potential problem might be.

The way I know the above quoted statement cannot be giving an error as-is, is if you simply do this Query:

```
SELECT datetime('now', '-24 hour');
```
you will see it is perfectly serviceable without any syntax errors.

This leads me to believe another part of the query was at fault. Here is the version that should work for you (unless I am again missing some fundamental difference with your specific setup):

```
SELECT * FROM btc_price_table WHERE btc_price_timestamp > datetime('now', '-24 hour');
```
So, assuming your language parser uses doubled-up quotes for escaping single quotes, your actual language statement might look like this:

```
c.execute('SELECT * FROM btc_price_table WHERE btc_price_timestamp > datetime(''now'', ''-24 hour'');')
```

If in stead it uses back-slash escaping, this might be the correct way:
```
c.execute('SELECT * FROM btc_price_table WHERE btc_price_timestamp > datetime(\'now\', \'-24 hour\');')
```

You get my drift - the resulting SQL is what is important and it must match the first statement above.

The '-24 hour' time-modifier can of course be replaced by many time-frames, such as '-1 hour' or '-30 minute' or '-1 month' etc.
More info here: [https://www.sqlite.org/lang_datefunc.html](sqlite.org/lang_datefunc.html)

If it doesn't work, please state exactly what is used and post the exact code you tried, someone here will real quick see what needs fixing for it to work.

Good luck!