date-arithmetic

Given a date range how to calculate the number of weekends partially or wholly within that range?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 19:46:57
Given a date range how to calculate the number of weekends partially or wholly within that range? (A few definitions as requested: take 'weekend' to mean Saturday and Sunday. The date range is inclusive i.e. the end date is part of the range 'wholly or partially' means that any part of the weekend falling within the date range means the whole weekend is counted.) To simplify I imagine you only actually need to know the duration and what day of the week the initial day is... I darn well now it's going to involve doing integer division by 7 and some logic to add 1 depending on the remainder but

Calculating in SQL the first working day of a given month

只谈情不闲聊 提交于 2019-12-04 17:52:36
I have to calculate all the invoices which have been paid in the first 'N' days of a month. I have two tables . INVOICE: it has the invoice information. The only field which does matter is called 'datePayment' . HOLYDAYS: It is a one column table. Entries at this table are of the form "2009-01-01", 2009-05-01" and so on. I should consider also Saturdays and Sundays (this might be not a problem because I could insert those days at the Hollidays table in order to consider them as hollidays if neccesary) The problem is to calculate which is the 'payment limit'. select count(*) from invoice where

PostgreSQL age() function: different/unexpected results when landing in dfferent month

穿精又带淫゛_ 提交于 2019-12-04 15:00:23
Today, I've encountered an unexplainable result in PostgreSQL 9.6 while running this query: SELECT age('2018-06-30','2018-05-19') AS one, age('2018-07-01','2018-05-20') AS two; Expected results for both columns: 1 mon 11 days . However, only for the interval from 2018-05-19 to 2018-06-30, I get what I expect, while for 2018-05-20 till 2018-07-01 I'll get one day more: 1 mon 12 days I don't get why this is the case and in my understanding, between 2018-05-20 2018-07-01 is just an interval of 1 mon 11 days and the Postgres result here is wrong. I cannot find any in-depth information on how

Bug in WeekNumber calculation .NET?

寵の児 提交于 2019-12-04 10:09:54
问题 I have a rather weird problem. I live in denmark and here the first week (Week 1) of 2013 starts the 31th of december 2012 and lasts for 7 days - as weeks normally do :) According to .NET however the 30th of december is Week 52, the 31th is Week 53 and the 1st of January is Week 1. Week 53 lasts for only one day, and Week 1 for 6 days. Clearly this must be wrong (a week consisting of less than 7 days) and certainly is wrong in danish context. Where the 31th of december is Week 1, NOT Week 53.

MySQL: Average interval between records

半城伤御伤魂 提交于 2019-12-03 11:02:00
问题 Assume this table: id date ---------------- 1 2010-12-12 2 2010-12-13 3 2010-12-18 4 2010-12-22 5 2010-12-23 How do I find the average intervals between these dates, using MySQL queries only? For instance, the calculation on this table will be ( ( 2010-12-13 - 2010-12-12 ) + ( 2010-12-18 - 2010-12-13 ) + ( 2010-12-22 - 2010-12-18 ) + ( 2010-12-23 - 2010-12-22 ) ) / 4 ---------------------------------- = ( 1 DAY + 5 DAY + 4 DAY + 1 DAY ) / 4 = 2.75 DAY 回答1: Intuitively, what you are asking

java calculate time between two timestamps

此生再无相见时 提交于 2019-12-03 09:00:47
问题 I need to calculate the time passed between two dates. The catch here is that I need to show it as YouTube does with its video comments timestamps. That is, to show it by just the largest measure. For example, if the time is 50 seconds ago it should say 50 seconds ago. if the time is more than one minute it should say one minute ago/ten minutes ago etc.. if the time difference is 1 hour 30 mins it should show: an hour ago. if the time is one and a half week than it should say one week ago. if

ORACLE SQL Date range intersections

浪子不回头ぞ 提交于 2019-12-03 07:26:21
问题 I have a table T1, it contains a NAME value (not unique), and a date range (D1 and D2 which are dates) When NAME are the same, we make a union of the date ranges (e.g. B). But as a result (X), we need to make intersection of all the date ranges Edit: Table T1 NAME | D1 | D2 A | 20100101 | 20101211 B | 20100120 | 20100415 B | 20100510 | 20101230 C | 20100313 | 20100610 Result : X | 20100313 | 20100415 X | 20100510 | 20100610 Visually, this will give the following : NAME : date range A : [-----

Bug in WeekNumber calculation .NET?

孤街醉人 提交于 2019-12-03 05:59:53
I have a rather weird problem. I live in denmark and here the first week (Week 1) of 2013 starts the 31th of december 2012 and lasts for 7 days - as weeks normally do :) According to .NET however the 30th of december is Week 52, the 31th is Week 53 and the 1st of January is Week 1. Week 53 lasts for only one day, and Week 1 for 6 days. Clearly this must be wrong (a week consisting of less than 7 days) and certainly is wrong in danish context. Where the 31th of december is Week 1, NOT Week 53. The following code illustrates the problem (CurrentCulture is "da-DK") static void Main(string[] args)

MySQL: Average interval between records

拈花ヽ惹草 提交于 2019-12-03 01:27:53
Assume this table: id date ---------------- 1 2010-12-12 2 2010-12-13 3 2010-12-18 4 2010-12-22 5 2010-12-23 How do I find the average intervals between these dates, using MySQL queries only? For instance, the calculation on this table will be ( ( 2010-12-13 - 2010-12-12 ) + ( 2010-12-18 - 2010-12-13 ) + ( 2010-12-22 - 2010-12-18 ) + ( 2010-12-23 - 2010-12-22 ) ) / 4 ---------------------------------- = ( 1 DAY + 5 DAY + 4 DAY + 1 DAY ) / 4 = 2.75 DAY Intuitively, what you are asking should be equivalent to the interval between the first and last dates, divided by the number of dates minus 1.

How to subtract hours from a date in Oracle so it affects the day also

心已入冬 提交于 2019-12-02 22:40:23
I'm trying to subtract date from Oracle so it even effect the day as well. For example, if the timestamp is 01/June/2015 00 hours and if I subtract 2 hours, I want to be able to go to to 31/May/2014 22 hours. I tried to_char(sysdate-(2/11), 'MM-DD-YYYY HH24') but it only subtracts the hour; it does not touch the day itself. Others have commented on the (incorrect) use of 2/11 to specify the desired interval. I personally however prefer writing things like that using ANSI interval literals which makes reading the query much easier: sysdate - interval '2' hour It also has the advantage of being