date-arithmetic

Using sql DATEADD function in java

时间秒杀一切 提交于 2019-11-28 05:12:35
问题 When I run queries using DATEADD it seems that the database does not recognize this function. also when I just run select DATEADD(Month, -3, GETDATE()) I'm getting: Error code -1, SQL state 42X01: Syntax error: Encountered "<EOF>" at line 1, column 36. I added the JAR file from hsqldb-2.2.9 as you can see What am I missing here? 回答1: Derby does not have a DATEADD function. You need to use the JDBC function timestampadd to achieve this: select {fn TIMESTAMPADD(SQL_TSI_MONTH, -3, CURRENT

How to get the end of a day?

こ雲淡風輕ζ 提交于 2019-11-28 04:08:39
问题 I'm using PostgreSQL 8.4 . I have a column of the table my_tbl which contains dates ( timestamp without timezone ). For instance: date ------------------- 2014-05-27 12:03:20 2014-10-30 01:20:03 2013-10-19 16:34:34 2013-07-10 15:24:26 2013-06-24 18:15:06 2012-07-14 07:09:14 2012-05-13 04:46:18 2013-01-04 21:31:10 2013-03-26 10:17:02 How to write an SQL query which returns all dates in the format: xxxx-xx-xx 23:59:59 That's every date will be set to the end of the day. 回答1: Take the date,

Subtract hours from the now() function

妖精的绣舞 提交于 2019-11-28 01:25:16
问题 We have a machine running 24x7. Every day I report the number of pieces it produced per hour. In our case one working day means '2015-06-16 06:00:00' to '2015-06-17 06:00:00' for example. Here is my code: select date_trunc('hour', t_el_eventlog.eventtime at time zone 'CET') as hours, count (distinct t_el_eventlog.serialnumber) as count from t_el_eventlog where eventtime at time zone 'CET' between '2015-06-16 06:00:00' and '2015-06-17 06:00:00' and sourceid = '44' group by hours order by hours

Number of days between two dates - ANSI SQL

核能气质少年 提交于 2019-11-27 23:16:27
I need a way to determine the number of days between two dates in SQL. Answer must be in ANSI SQL. Monkey Boson ANSI SQL-92 defines DATE - DATE as returning an INTERVAL type. You are supposed to be able to extract scalars from INTERVALS using the same method as extracting them from DATEs using – appropriately enough – the EXTRACT function (4.5.3). <extract expression> operates on a datetime or interval and returns an exact numeric value representing the value of one component of the datetime or interval. However, this is very poorly implemented in most databases. You're probably stuck using

Arithmetics on calendar dates in C or C++ (add N days to given date)

北城以北 提交于 2019-11-27 23:14:50
I have been given a date, Which I am taking as an input like (day, month, year): 12, 03, 87 . Now I need to find out the date after n days. I have written code for this, But its not efficient. Can you please tell me any good logic which works faster and have less complexity. #include <stdio.h> static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day, month, year; unsigned short day_counter; int is_leap(int y) { return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0); } next_day() { day += 1; day_counter++; if (day > days_in_month[month]) { day = 1; month += 1;

How to add a time interval to an NSDate?

亡梦爱人 提交于 2019-11-27 22:30:13
I have an NSDate and a duration. I need to get the time after the duration Given: The date is "2010-02-24 12:30:00 -1000" duration is 3600 secs I need to get "2010-02-24 13:30:00 -1000" I thought dateWithTimeIntervalSinceReferenceDate: , would do the trick but I see now that this gives a date offset from 1 Jan 2001 GMT. Is there another C function I need to use As DyingCactus states, you can use the addTimeInterval method of NSDate, but depending on the OS version is will create a compiler warning, since it is deprecated in 10.6 as of 2009-08-17. The current recommended method is to use

How to subtract 2 dates in oracle to get the result in hour and minute

人走茶凉 提交于 2019-11-27 22:20:28
问题 I want to subtract 2 dates and represent the result in hour and minute in one decimal figure. I have the following table and I am doing it in this way but the result is not as desired. There is some slight variation, I'm sure this is simple arithmetic but I'm not getting it right. select start_time, end_time, (end_time-start_time)*24 from come_leav; START_TIME END_TIME (END_TIME-START_TIME)*24 ------------------- ------------------- ------------------------ 21-06-2011 14:00:00 21-06-2011 16

Get the difference between two dates both In Months and days in sql

血红的双手。 提交于 2019-11-27 21:41:05
问题 I need to get the difference between two dates say if the difference is 84 days, I should probably have output as 2 months and 14 days, the code I have just gives the totals. Here is the code SELECT Months_between(To_date('20120325', 'YYYYMMDD'), To_date('20120101', 'YYYYMMDD')) num_months, ( To_date('20120325', 'YYYYMMDD') - To_date('20120101', 'YYYYMMDD') ) diff_in_days FROM dual; Output is: NUM_MONTHS DIFF_IN_DAYS 2.774193548 84 I need for example the output for this query to be either 2

How to get age in years,months and days using Oracle

▼魔方 西西 提交于 2019-11-27 18:46:13
问题 I'm trying to print for each person its age using this format : E.g : 19 years , 8 months , 13 days. I've googled a lot and I've noticed that there is a specific function to calculate the difference between dates DATEDIFF . However this function does not exist in SQL*Plus , so I went on trying using MONTHS_BETWEEN() and some operators. My attempt: SELECT name , ' ' || FLOOR(MONTHS_BETWEEN(to_date(SYSDATE),to_date(date_of_birth))/12)||' years ' || FLOOR(MOD(MONTHS_BETWEEN(to_date(SYSDATE),to

Oracle date “Between” Query

帅比萌擦擦* 提交于 2019-11-27 18:27:58
I am using oracle database. i want to execute one query to check the data between two dates. NAME START_DATE ------------- ------------- Small Widget 15-JAN-10 04.25.32.000000 PM Product 1 17-JAN-10 04.31.32.000000 PM select * from <TABLENAME> where start_date BETWEEN '15-JAN-10' AND '17-JAN-10' But I dont get any results from above query. I think I have to use "like" and "%". But I dont know where to use them. Please throw some lights on this. thanks in advance. Judging from your output it looks like you have defined START_DATE as a timestamp. If it were a regular date Oracle would be able to