Delete expired products from SQLite and treeview - sqlite

I want to delete elements at today's date. But this statement doesn't work.
cur.execute("DELTE FROM medicine WHERE date DATE('now') AND DATE('now', '-1 day')")
try:
tree.delete(cur)
except IndexError:
pass

If the column's date format is YYYY-MM-DD then you need this statement:
DELETE FROM medicine WHERE date = DATE('now')
or:
DELETE FROM medicine WHERE date = CURRENT_DATE

Related

SQLite last 24h query with 2 rows date and time

I want to query last 24h in my database SQLite. I have 2 rows time example (18:04:56) and date (2020-12-06 ). When I use this query
SELECT name,cdate,ctime, * FROM {table} WHERE cdate >= datetime('now','-1 day') ORDER BY id DESC
I got only last queries in the same day. (LAST QUERY 00:00:46)
How deal with it ?
You should create a datetime value from cdate and ctime to compare with DATETIME('now','-1 day'):
SELECT name, cdate, ctime
FROM {table}
WHERE (cdate || ' ' || ctime) >= DATETIME('now','-1 day')
ORDER BY id DESC

how to delete last 2 days entry form a table in sqlite?

i m trying like this:
char *Query = "DELETE FROM tablename WHERE starttime < NOW() - INTERVAL 1 DAY";
First,to delete last 2 days,you need to check data greater than the specified date,then you need to use DATE('now', '-1 days') to get the date,more details can be found at How to structure my query in sqlite?
DELETE FROM bandwidthmaster WHERE starttime > DATE('now', '-1 days');

Select weekend or weekday data from a table based on date param

How can I select data from a table based on weekday or weekend, like
if date is a weekday then select only historical weekday data from the table &
if date is a weekend then select only historical weekend data.
I have tried to do that in this way but no luck
DECLARE #MyDate DATE = '08/17/2013'
SELECT datename(dw,#MyDate)
SELECT * FROM MyTable
WHERE
datename(dw,DateColumnInTable) IN (
CASE WHEN (datename(dw,#MyDate) IN ('Saturday','Sunday')) THEN '''Saturday'',''Sunday'''
ELSE 'Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'
END )
Any I can see lots of data in my table for saturday and sunday but this query is giving me blank record set.
Here's one way:
DECLARE #MyDate DATE = '08/17/2013'
IF (DATEPART(weekday, #MyDate) IN (1,7))
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) IN (1,7)
ELSE
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6
If you would like to do it in one clause you can do something like the following, but it may perform worse:
SELECT *
FROM MyTable
WHERE (DATEPART(weekday, #MyDate) IN (1,7) AND DATEPART(weekday, DateColumnInTable) IN (1,7))
OR (DATEPART(weekday, #MyDate) BETWEEN 2 AND 6 AND DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6)

Add numbers of day to Date SqLite

I need add +1 Day, to a selectec Date, in Slq the sintax is: SELECT DATEADD(day,+1, period.DateEnd) and it works, but in sqLite es different.
I try with this, but it doesn't work, for example, the DateEnd = '31/12/2012', I need add 1 day to that date, te resulset should be: DateEnd = '01/01/2013'
SELECT date('period2.DateEnd', '+1 day') as date
FROM Period as period2
WHERE period2.c_Pk_CodPeriod = '1012'
Currently you've got period2.DateEnd as a string. I suspect you want:
SELECT date(period2.DateEnd, '+1 day') as date
FROM Period as period2
WHERE period2.c_Pk_CodPeriod = '1012'

SQL return result if where clause doesn't match anything

I have a database with two tables: trips and days. Each entry in days has an id that corresponds to an entry in trips.
I want to run a query that will return the trip that occurs on a provided date, and if there isn't one that occurs on that date then return the next trip after that date. Here's my current query (daystring is generated in code, assume it's a valid SQL date string):
SELECT _id, number, date
FROM trips
INNER JOIN days ON trips._id = days.trips_id
WHERE days.date > DATE( daystring, "start of day", "-1 day" )
AND days.date < DATE( daystring, "start of day", "+1 day" );
That query only returns a result if a trip exists on the provided date though. How can I modify it to return the next trip after that date only if there isn't one on that date?
SELECT _id, number, date FROM trips INNER JOIN days ON trips._id = days.trips_id
WHERE days.date > DATE( daystring, "start of day", "-1 day" )
order by date Limit 1;
is a quick fix. Might want an index on the date column if you don't have one though.

Resources