Questions tagged [date-math]
Use for queries that calculate dates or date ranges
35 questions
10 votes
2 answers
952 views
DATEDIFF Rounding
Implementing a rotating partition scheme, per kejser.org/table-pattern-rotating-log-ring-. Ran into an issue with DATEDIFF rounding up values: DECLARE @Partitions INT = 15; SELECT a1.dt , ...
-2 votes
1 answer
74 views
Using sum and datediff from 2 tables [duplicate]
I am using 2 tables Members (MemberID, MemberName, Department, JoinDate, LeaveDate) Subscriptions (ID, MemberName, DatePaid, AmountPaid) I have combined them SELECT Members.MemberID, Members....
0 votes
0 answers
74 views
Create a group number for rolling month
Starting from a table with dates in descending order, starting from the current_date: date ---------- 2023-02-03 2023-02-02 2023-02-01 2023-01-31 ... I want to add a column that contains a group ...
1 vote
2 answers
524 views
Calculating the total time a device is on per day
At the moment I've got an sqlite3 database that keeps track of the state of my smart home devices. The relevant parts of the schema for the main table are CREATE TABLE states( state_id INTEGER NOT ...
5 votes
2 answers
2k views
Records greater than epoch timestamp using only LIKE operator
I have the following query so far and unfortunately, I cannot use regexp or greater than operators, I can only use the LIKE keyword. The whole column is in a json string, I can't use json_value or ...
4 votes
1 answer
14k views
Recommended way to index a timestamp column in postgres?
I have a PostgreSQL table called tickets_details, which has many columns and is constantly updated only on the rows of the current day, it also inserts thousands of rows of the current day that have ...
11 votes
3 answers
4k views
Add default rows to the query result if restrictions are not met
I have a SQL query that looks for values in a table on a range of dates. If no record is found, I would like to generate rows with default values. Example of one of the table existing records: ...
1 vote
1 answer
2k views
Postgres find gaps between one date range and others
My question is similar to https://stackoverflow.com/a/41267928/2585154 , but in my case I need to divide/split date range by multiple date ranges, not only one. In my case is also guaranteed that &...
1 vote
2 answers
1k views
Getting the first Monday after the third Friday
I have seen on this site how to get the the third Friday of the month: select quote_date FROM table where datename(weekday, quote_date) = 'Friday' and datepart(day, quote_date)>=15 and datepart(...
2 votes
2 answers
2k views
Explode a date range
I have the following table with the following data: DECLARE @MyActions TABLE (ActionId INT NOT NULL, ActionDate DATETIME NOT NULL) INSERT INTO @MyActions VALUES (1, '2021-08-01 01:00:00') INSERT INTO ...
5 votes
1 answer
813 views
Standard Deviation for Times
Is there a T-SQL coding best practice to get an accurate standard deviation value for a group of times? STDEV doesn't like the time data type. I'm thinking that maybe a conversion to minutes as an ...
1 vote
1 answer
7k views
How to calculate the number of days between two dates?
I'm trying to calculate days between two dates. Let's say I have the date '2005-05-25 06:04:08' and I want to count how many days are there between that day and the current date. I tried doing: SELECT ...
0 votes
3 answers
13k views
Sum date between two date and group by month
I have a table as below: Name Start Date End Date Joe 20/04/2021 20/05/2021 John 01/05/2021 28/05/2021 I am using a SQL table-valued function to return a table that has 2 columns: Month and ...
2 votes
1 answer
1k views
Office Hours in PostgreSQL
How to store office hours in PostgreSQL rows, so that I can calculate the office hours. Example: We have open from 9:00 to 18:00 from Monday till Friday. Saturday we have open from 10:00 to 15:00 ...
0 votes
1 answer
664 views
count of rows in table day wise
I need to get a count of rows in a table day wise. The table looks like below: Table ABC: f_id|reg_date 1|2020-09-08 2|2020-09-12 3|2020-10-01 4|2020-09-07 5|2020-09-08 6|2020-09-09 Expected output ...