date-arithmetic

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

我怕爱的太早我们不能终老 提交于 2019-11-29 01:32:09
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 months and 14 days at worst, otherwise I won't mind if I can have the exact days after the months figure

How to calculate an age based on a birthday? [duplicate]

依然范特西╮ 提交于 2019-11-28 23:09:48
Possible Duplicate: How do I calculate someone’s age in C#? I want to write an ASP.NET helper method which returns the age of a person given his or her birthday. I've tried code like this: public static string Age(this HtmlHelper helper, DateTime birthday) { return (DateTime.Now - birthday); //?? } But it's not working. What is the correct way to calculate the person's age based on their birthday? Pierre-Alain Vigeant Stackoverflow uses such function to determine the age of a user. Calculate age in C# The given answer is DateTime now = DateTime.Today; int age = now.Year - bday.Year; if (now <

Oracle DateTime in Where Clause?

爱⌒轻易说出口 提交于 2019-11-28 16:12:06
问题 I have sql something like this: SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy') -> This returns 10 rows and TIME_CREATED = '26-JAN-2011' Now when i do this i don't get any rows back, SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED = TO_DATE('26/JAN/2011','dd/mon/yyyy') -> Took the greater than out Any reason why? 回答1: Yes: TIME_CREATED contains a date and a time . Use TRUNC to strip the time: SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TRUNC

Prevention triggers

不羁的心 提交于 2019-11-28 14:00:24
问题 I have a table like this: StudentID Student Name Birthdate Student Birthplace Gender Height Weight --------- --------------- --------- ------------------ ------ ------ ------ 83 Adam Stone 30-JUN-94 Towson, USA M 193 88 84 Stephanie Love 17-JUN-93 KL,Malaysia F 176 67 85 Rachel Kim 17-FEB-92 Seoul, South Korea F 179 56 How do i write a trigger to prevent any student under the age of 15 from being stored in the student's table? 回答1: You have a Date of Birth. So you need to determine that the

Add 2 months to current timestamp

五迷三道 提交于 2019-11-28 13:49:21
How can I add months to a timestamp value in Oracle? In my query, it's getting converted to date value instead: SELECT add_months(current_timestamp,2) FROM dual; The actual output is: ADD_MONTH 11-MAR-13 The expected output is: 2013-01-01 00:00:00.000000000+00:00 This will give you the date and the time as a TIMESTAMP data type: select TO_TIMESTAMP(TO_CHAR(ADD_MONTHS(SYSDATE, 2), 'YYYYMMDD HH24:MI'), 'YYYYMMDD HH24:MI') from dual; If you need more or less precision (E.G. rounding) than what is above, adjust the date formats (both need to be the same format). For example, this will return 2

How to count days except Sundays between two dates in Postgres?

空扰寡人 提交于 2019-11-28 12:56:35
To find the number of days between two dates we can use something like this: SELECT date_part('day',age('2017-01-31','2017-01-01')) as total_days; In the above query we got 30 as output instead of 31. Why is that? And I also want to find the number of days except Sundays. Expected output for the interval ('2017-01-01', '2017-01-31') : Total Days = 31 Total Days except Sundays = 26 You need to define "between two dates" more closely. Lower and upper bound included or excluded? A common definition would be to include the lower and exclude the upper bound of an interval. Plus, define the result

ISO week number in CMD

时间秒杀一切 提交于 2019-11-28 11:25:16
How can I determine the ( ISO 8601 ) week number in a Windows batch file? Unfortunately WMIC PATH WIN32_LOCALTIME GET /FORMAT:LIST only has WeekInMonth ... I have found some very complex solutions . Is there no easier way? You can use Ritchie Lawrences's Date Functions. It is maintained on Gitub. https://ritchielawrence.github.io/batchfunctionlibrary/ Here is the code. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :DateToWeek %yy% %mm% %dd% yn cw dw :: :: By: Ritchie Lawrence, Updated 2002-11-20. Version 1.1 :: :: Func: Returns an ISO 8601 Week date from a

How to get a list of months between 2 given dates using a query?

故事扮演 提交于 2019-11-28 11:15:02
I have 2 dates, say 28-Mar-2011 and 29-Jun-2011. I need an sql query that will display the months between these 2 dates including the months containing the dates, ie. June, May, April and March. Justin Cave Something like this SQL> ed Wrote file afiedt.buf select to_char( add_months( start_date, level-1 ), 'fmMonth' ) from (select date '2011-03-30' start_date, date '2011-06-29' end_date from dual) connect by level <= months_between( trunc(end_date,'MM'), trunc(start_date,'MM') ) * + 1 SQL> / TO_CHAR(ADD_MONTHS(START_DATE,LEVEL- ------------------------------------ March April May June should

Calculate Difference between dates by group in R

丶灬走出姿态 提交于 2019-11-28 10:18:26
问题 I'm using a logistic exposure to calculate hatching success for bird nests. My data set is quite extensive and I have ~2,000 nests, each with a unique ID ("ClutchID). I need to calculate the number of days a given nest was exposed ("Exposure"), or more simply, the difference between the 1st and last day. I used the following code: HS_Hatch$Exposure=NA for(i in 2:nrow(HS_Hatch)){HS_Hatch$Exposure[i]=HS_Hatch$DateVisit[i]- HS_Hatch$DateVisit[i-1]} where HS_Hatch is my dataset and DateVisit is

Comparing date with sysdate in oracle

ぃ、小莉子 提交于 2019-11-28 07:41:50
问题 I have a column which is of " DATE " type and I want to run a query on it comparing it with sysdate. But I am getting following error, Can someone please let me know what I am missing here? SQL> select distinct file_name as r from table_1 where view_day >= TO_DATE(SYSDATE-10, 'YYYY/MM/DD'); ERROR at line 1: ORA-01858: a non-numeric character was found where a numeric was expected 回答1: You shouldn't use to_date on a date, To_date is for casting a varchar to date, not a date. If you do use the