Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: how to add minutes to a timestamp?

I need to add 30 minutes to values in a Oracle date column. I do this in my SELECT statement by specifying

to_char(date_and_time + (.000694 * 31)

which works fine most of the time. But not when the time is on the AM/PM border. For example, adding 30 minutes to 12:30 [which is PM] returns 1:00 which is AM. The answer I expect is 13:00. What's the correct way to do this?

like image 849
Sajee Avatar asked Nov 02 '08 20:11

Sajee


People also ask

How do I add minutes to a TIMESTAMP in SQL?

We can use DATEADD() function like below to add minutes to DateTime in Sql Server. DATEADD() functions first parameter value can be minute or mi or n all will return the same result.

How do I add minutes in SQL Developer?

SELECT to_char(sysdate + (1/24/60) * 30, 'dd/mm/yy HH24:MI am') from dual; simply you can use this with various date format....

How do I add a TIMESTAMP in Oracle?

You can use the below code: insert into tablename (timestamp_value) values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS')); If you need the current timestamp to be inserted then use the following code: insert into tablename (timestamp_value) values (CURRENT_TIMESTAMP);

How do I add time to Sysdate?

You can also add 5 minutes to a date this way: select sysdate, sysdate + interval '5' minute from dual; As we see, there are several ways to add minutes to an Oracle date column.


Video Answer


2 Answers

In addition to being able to add a number of days to a date, you can use interval data types assuming you are on Oracle 9i or later, which can be somewhat easier to read,

SQL> ed Wrote file afiedt.buf SELECT sysdate, sysdate + interval '30' minute FROM dual SQL> /  SYSDATE              SYSDATE+INTERVAL'30' -------------------- -------------------- 02-NOV-2008 16:21:40 02-NOV-2008 16:51:40 
like image 166
Justin Cave Avatar answered Sep 19 '22 15:09

Justin Cave


All of the other answers are basically right but I don't think anyone's directly answered your original question.

Assuming that "date_and_time" in your example is a column with type DATE or TIMESTAMP, I think you just need to change this:

to_char(date_and_time + (.000694 * 31)) 

to this:

to_char(date_and_time + (.000694 * 31), 'DD-MON-YYYY HH24:MI') 

It sounds like your default date format uses the "HH" code for the hour, not "HH24".

Also, I think your constant term is both confusing and imprecise. I guess what you did is calculate that (.000694) is about the value of a minute, and you are multiplying it by the number of minutes you want to add (31 in the example, although you said 30 in the text).

I would also start with a day and divide it into the units you want within your code. In this case, (1/48) would be 30 minutes; or if you wanted to break it up for clarity, you could write ( (1/24) * (1/2) ).

This would avoid rounding errors (except for those inherent in floating point which should be meaningless here) and is clearer, at least to me.

like image 36
Dave Costa Avatar answered Sep 17 '22 15:09

Dave Costa