date-arithmetic

SQL: Is it possible to SUM() fields of INTERVAL type?

北城以北 提交于 2019-12-01 20:10:48
问题 I am trying to sum INTERVAL. E.g. SELECT SUM(TIMESTAMP1 - TIMESTAMP2) FROM DUAL Is it possible to write a query that would work both on Oracle and SQL Server? If so, how? Edit: changed DATE to INTERVAL 回答1: I'm afraid you're going to be out of luck with a solution which works in both Oracle and MSSQL. Date arithmetic is something which is very different on the various flavours of DBMS. Anyway, in Oracle we can use dates in straightforward arithmetic. And we have a function NUMTODSINTERVAL

Getting Last Day of Previous Month in Oracle Function

自古美人都是妖i 提交于 2019-12-01 17:20:10
I need a function in Oracle like this. When i giving a parameter a simple date. Then function should getting me last day of the previous month. Example: FunctionName(10.02.2011) Result should be 31.01.2011 FunctionName(21.03.2011) Result should be 28.02.2011 FunctionName(31.07.2011) Result should be 30.06.2011 (Even date is last day of month) How can i do that? By the way, i never use Oracle . Lamak SELECT LAST_DAY(ADD_MONTHS(yourdate,-1)) As an alternative to the other answers here, you can also find the last day of the previous month by getting the day before the first day of this month

Fill missing date values in column by adding delivery interval to another date column

為{幸葍}努か 提交于 2019-12-01 12:58:08
问题 Data: DB1 <- data.frame(orderItemID = 1:10, orderDate = c("2013-01-21","2013-03-31","2013-04-12","2013-06-01","2014-01-01", "2014-02-19","2014-02-27","2014-10-02","2014-10-31","2014-11-21"), deliveryDate = c("2013-01-23", "2013-03-01", "NA", "2013-06-04", "2014-01-03", "NA", "2014-02-28", "2014-10-04", "2014-11-01", "2014-11-23")) Expected Outcome: DB1 <- data.frame(orderItemID = 1:10, orderDate= c("2013-01-21","2013-03-31","2013-04-12","2013-06-01","2014-01-01", "2014-02-19","2014-02-27",

How to add a running count to rows in a 'streak' of consecutive days

匆匆过客 提交于 2019-11-30 22:23:45
Thanks to Mike for the suggestion to add the create/insert statements. create table test ( pid integer not null, date date not null, primary key (pid, date) ); insert into test values (1,'2014-10-1') , (1,'2014-10-2') , (1,'2014-10-3') , (1,'2014-10-5') , (1,'2014-10-7') , (2,'2014-10-1') , (2,'2014-10-2') , (2,'2014-10-3') , (2,'2014-10-5') , (2,'2014-10-7'); I want to add a new column that is 'days in current streak' so the result would look like: pid | date | in_streak -------|-----------|---------- 1 | 2014-10-1 | 1 1 | 2014-10-2 | 2 1 | 2014-10-3 | 3 1 | 2014-10-5 | 1 1 | 2014-10-7 | 1 2

Calculate difference between 2 times in hours in PHP

跟風遠走 提交于 2019-11-30 22:08:29
I have two times - For eg- the current time - 08:24 and date is 02/01/2013 in dd/mm/yyyy format and I have another time at 13:46 and date is 31/12/2012 . So, how can I calculate the difference between the 2 times in hours using PHP. (i.e. 42.63 hours) Thanks in advance. Convert them both to timestamp values, and then subtract to get the difference in seconds. $ts1 = strtotime(str_replace('/', '-', '02/01/2013 08:24')); $ts2 = strtotime(str_replace('/', '-', '31/12/2012 13:46')); $diff = abs($ts1 - $ts2) / 3600; Another way is to use PHP's date-related classes. The example below uses DateTime:

How to add a running count to rows in a 'streak' of consecutive days

倖福魔咒の 提交于 2019-11-30 17:16:00
问题 Thanks to Mike for the suggestion to add the create/insert statements. create table test ( pid integer not null, date date not null, primary key (pid, date) ); insert into test values (1,'2014-10-1') , (1,'2014-10-2') , (1,'2014-10-3') , (1,'2014-10-5') , (1,'2014-10-7') , (2,'2014-10-1') , (2,'2014-10-2') , (2,'2014-10-3') , (2,'2014-10-5') , (2,'2014-10-7'); I want to add a new column that is 'days in current streak' so the result would look like: pid | date | in_streak -------|-----------|

How to get the closest dates in Oracle sql

社会主义新天地 提交于 2019-11-30 16:56:54
问题 For example, I have 2 time tables: T1 id time 1 18:12:02 2 18:46:57 3 17:49:44 4 12:19:24 5 11:00:01 6 17:12:45 and T2 id time 1 18:13:02 2 17:46:57 I need to get time from T1 that are the closest to time from T2. There is no relationship between this tables. It should be something like this: select T1.calldatetime from T1, T2 where T1.calldatetime between T2.calldatetime-( select MIN(ABS(T2.calldatetime-T1.calldatetime)) from T2, T1) and T2.calldatetime+( select MIN(ABS(T2.calldatetime-T1

Create View with 365 days

送分小仙女□ 提交于 2019-11-30 08:45:48
问题 How to Create a View with all days in year. view should fill with dates from JAN-01 to Dec-31. How can I do this in Oracle ? If current year have 365 days, view should have 365 rows with dates. if current year have 366 days, view should have 366 rows with dates. I want the view to have a single column of type DATE . 回答1: This simple view will do it: create or replace view year_days as select trunc(sysdate, 'YYYY') + (level-1) as the_day from dual connect by level <= to_number(to_char(last_day

SQL: Get records created in time range for specific dates

断了今生、忘了曾经 提交于 2019-11-30 07:04:51
I have a set of records that were created last week, and from those I want to retrieve only those that were created between 6h45 and 19h15. I have a column creation_date that I can use. How can I do this in sql? In Oracle we can turn dates into numbers and apply arithmetic to them in a variety of ways. For instance sysdate-7 gives us the date seven days ago. trunc(some_date) removes the time element from a date column. And to_char(some_date, 'SSSSS') gives us its time element as the number of seconds since midnight. So 06:45:00 is 24300 seconds and 18:15:59 is 69359 seconds (please check those

Calculate the week number (0-53) in year

偶尔善良 提交于 2019-11-30 06:47:25
I have a dataset with locations and dates. I would like to calculate week of the year as number (00–53) but using Thursday as the first day of the week. The data looks like this: location <- c(a,b,a,b,a,b) date <- c("04-01-2013","26-01-2013","03-02-2013","09-02-2013","20-02-2013","03-03-2013") mydf <- data.frame(location, date) mydf I know that there is strftime function for calculating week of year but it is only possible to use Monday or Sunday as the first day of the week. Any help would be highly appreciated. Just add 4 to the Date-formatted values: > mydf$Dt <- as.Date(mydf$date, format="