Add numbers of day to Date SqLite - 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'

Related

Expand months between start date and end date in teradata

Need Query to expand the months between the start date and end date.
Below is one example of data.
Result is the output I would like to see.
Any help is much appreciated.
Example columns:
ID start date, End date
300 08/20/2021 10/25/2021
Result:
ID, Dateexpand
300 08/2021
300 09/2021
300 10/2021
Using Teradata's EXPAND on syntax (documentation):
select
id,
start_date,
end_Date,
prd,
last(prd) as end_month, --this will return the last date in the period
to_char(last(prd),'YYYYMM')
from
<your table>
expand on period (start_date,end_date) as prd BY ANCHOR PERIOD MONTH_BEGIN
I left all the columns in there so you can see what the PERIOD returns.
EDIT:
Period will not work if the ending date is not greater than the starting date. If you have cases where this isn't true, you'll have to process those separately and union them together:
select
id,
to_char(last(prd),'YYYYMM')
from
<your table>
where end_date > start_date
expand on period (start_date,end_date) as prd BY ANCHOR PERIOD MONTH_BEGIN
union
select
id,
to_char(start_date, 'YYYYMM')
from
<your table>
where end_date = start_date
order by 1,2

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

Delete expired products from SQLite and treeview

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

SQL SELECT query between 2 unknown dates

I was trying to find symptoms between 2 dates. The date is obtained from a table and is not current date. The range is date +1 day to date+ 5 days. I wrote this code but it does not seem to work.
Where am I going wrong?
SELECT answer
FROM column
WHERE BETWEEN date(date,'+1 day') AND date(date, '+5 days')
--maybe like this :)
--dateadd to +5 days
SELECT x
FROM date a
WHERE a.yourDate BETWEEN a.yourDate and DateAdd(day, 5, a.yourDate)
--using GateDate();
SELECT x
FROM date a
WHERE a.yourDate BETWEEN getdate() and DateAdd(day, 5, getdate())

How to count days between date range with a specifific day?

How to count days between date range with a specific day?
Example:
START_DT = January 1, 2014;
END_DT = January 31, 2014;
Day = :SampleDay
Sample Result:
Monday = 4,
Tuesday = 4,
Wednesday = 5
Please help. :|
Are you looking for something like this,
WITH t(date1, date2) AS
(
SELECT to_date('01/01/2014', 'dd/mm/yyyy'),
to_date('31/01/2014','dd/mm/yyyy')+1 -- Adding 1 to calculate the last day too.
FROM DUAL
)
SELECT count(days) day_count, day
DAY
FROM(
SELECT date1 + LEVEL -1 days,
to_char(date1 + LEVEL -1, 'FmDay') DAY, --Use `FmDay`, this will remove the Embedded spaces.
to_char(date1 + LEVEL -1, 'D') DAY#
FROM t
CONNECT BY LEVEL <= date2 - date1
)
WHERE day = 'Monday' --Filter with day, if you want to get the count for a specific day.
GROUP BY DAY, day#
ORDER BY day#;
You wont have a direct solution to this. In oracle you have this form to know what day of the week is a specific date:
to_char(to_date('01012014', 'ddmmyyyy'), 'Day')
I would recommend to you to make a store procedure with a simple algorithm which receive that three parameters and then display the information you need. Put it in a query and it is done.

Resources