given a date in sqllite I would like to find the last weekday of the month.
for example, if given 11/17/2021 then the last weekday of the month is 11/30. however, if given 4/30/2022 which falls on a saturday then the last weekday of the month is 4/29/2022.
i try the following but this only gives me the last day of the month which can be either a weekend of weeday.
SELECT date('now','start of month','+1 month','-1 day');
i am looking for the last weekday of the month given a specific date in sql lite.
can someone help me figure this out? thanks in advance
One solution is to calculate the day of the week for the end of the month, and adjust the result based off that value:
SELECT
CASE strftime('%w', date('now', 'start of month','+1 month','-1 day'))
WHEN '0' THEN date('now', 'start of month','+1 month','-3 day')
WHEN '6' THEN date('now', 'start of month','+1 month','-2 day')
ELSE date('now', 'start of month','+1 month','-1 day')
END;
The following will take a US formatted date (m/d/y) and output the last weekday in the month in the same US format:-
WITH
cte_inputdate AS (SELECT '12/19/2022' /*<<<<<<<<<<< change as required */ AS inputdate),
cte_convertdate AS
(SELECT
CASE
/* m/d/yyyy */
WHEN substr(inputdate,2,1) = '/'
AND substr(inputdate,4,1) = '/'
THEN substr(inputdate,5,4)||'-0'||substr(inputdate,1,1)||'-0'||substr(inputdate,3,1)
/* m/dd/yyyy */
WHEN substr(inputdate,2,1) = '/'
AND substr(inputdate,5,1) = '/'
THEN substr(inputdate,6,4)||'-0'||substr(inputdate,1,1)||'-'||substr(inputdate,3,2)
/* mm/d/yyyy */
WHEN substr(inputdate,3,1) = '/'
AND substr(inputdate,5,1) = '/'
THEN substr(inputdate,6,4)||'-'||substr(inputdate,1,2)||'-0'||substr(inputdate,4,1)
/* mm/dd/yyyy */
ELSE substr(inputdate,7,4)||'-'||substr(inputdate,1,2)||'-'||substr(inputdate,4,2)
END AS inputdate
FROM cte_inputdate
),
cte_lastweekdayofmonth AS
(SELECT *,
CASE CAST(strftime('%w',inputdate,'start of month','+1 month','-1 day') AS INTEGER)
WHEN 0 THEN date(inputdate,'start of month','+1 month','-3 day')
WHEN 6 THEN date(inputdate,'start of month','+1 month','-2 day')
ELSE date(inputdate,'start of month','+1 month','-1 day')
END AS lastweekdayofmonth
FROM cte_convertdate
)
/* Extract lastweekdayofthemonth converting it to m/d/yyyy format */
SELECT
CASE WHEN substr(lastweekdayofmonth,6,1) = '0' THEN substr(lastweekdayofmonth,7,1) ELSE substr(lastweekdayofmonth,6,2) END||'/'||
CASE WHEN substr(lastweekdayofmonth,9,1) = '0' THEN substr(lastweekdayofmonth,10,1) ELSE substr(lastweekdayofmonth,9,2) END||'/'||
substr(lastweekdayofmonth,1,4) AS lastweekdayofmonth
FROM cte_lastweekdayofmonth
;
e.g
for 11/17/2021 then :-
for 4/30/2022 then :-
Would this be a correct/fast way to check if all days from the previous month are in my table? My Snowflake table has a 'date' column that is a 'date' type:
I'm doing this but I feel there must be a better way?
SELECT *
FROM dfp.revenue_allocation
WHERE YEAR("date") = '{{ execution_date.year }}'
AND MONTH("date") = '{{ execution_date.month }}'-1
To get best performance, expression on columns should be avoided:
SELECT *
FROM dfp.revenue_allocation
WHERE "date" >= TRUNC( CAST(? AS DATE) - INTERVAL '1 MONTH', 'MONTH')
AND "date" < TRUNC( CAST(? AS DATE), 'MONTH');
TRUNC
Truncates a date, time, or timestamp to the specified part
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
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
I wish to create a report that would list all the tickets that were closed in a certain period of time.
The pseudo-code would be like
SELECT * FROM tickets
WHERE closed AND date_closed = 'january 2009'
The part I am unable to resolve is date_closed = 'january 2009'.
Is there a way to do it in Trac?
I am not interested in particular SQL syntax, I can write the time constrictions myself. What I am not certain about is Trac's db structure.
SELECT * FROM ticket
WHERE status='closed'
AND date(changetime,'unixepoch')
BETWEEN date('YYYY-MM-DD') /* <- here goes your start date */
AND date('YYYY-MM-DD') /* <- here goes your end date */
If you want a specific month:
SELECT * FROM ticket
WHERE status='closed'
AND date(changetime,'unixepoch')
BETWEEN date('2009-01-01','start of month')
AND date('2009-01-01','start of month','+1 month','-1 day')
Where date('2009-01-01','start of month') is the first day of the month given by date, and date('2009-01-01','start of month','+1 month','-1 day') is the last day of the month.
SELECT DISTINCT ticket.* FROM ticket, ticket_change
WHERE ticket.id = ticket_change.ticket
AND ticket_change.field = 'status'
AND ticket_change.newvalue = 'closed'
AND strftime('%m', ticket_change.time, 'unixepoch') = '01';
If you also know the year, instead of strftime you’d better use an expression like vartec’s suggested:
SELECT DISTINCT ticket.* FROM ticket, ticket_change
WHERE ticket.id = ticket_change.ticket
AND ticket_change.field = 'status'
AND ticket_change.newvalue = 'closed'
AND date(ticket_change.time,'unixepoch')
BETWEEN date('2009-01-01','start of month')
AND date('2009-01-01','start of month','+1 month','-1 day')
Also, regarding the table structure, here you go:
CREATE TABLE ticket_change (
ticket INTEGER,
time INTEGER,
author TEXT,
field TEXT,
oldvalue TEXT,
newvalue TEXT,
UNIQUE ( ticket, time, field )
);