How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.
select '01-jan-2011 11-09-05' + interval '2' day
A completely Oracle-centric solution is to simply add 2 to the timestamp value as the default interval is days for Oracle dates/timestamps:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + 2
FROM dual;
In a similar case, I used:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + NUMTODSINTERVAL(2, 'DAY')
Because, othewise, the expression is converted to DATE and precission is lost. See: NUMTODSINTERVAL documentation
Related
How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.
select '01-jan-2011 11-09-05' + interval '2' day
A completely Oracle-centric solution is to simply add 2 to the timestamp value as the default interval is days for Oracle dates/timestamps:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + 2
FROM dual;
In a similar case, I used:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + NUMTODSINTERVAL(2, 'DAY')
Because, othewise, the expression is converted to DATE and precission is lost. See: NUMTODSINTERVAL documentation
I have a timestamp that comes in such from: 2021-12-20T18:00:55.126X
I want to change the form and show only the date and time so it would become something like 2021-12-20 18:00:55 The challenge is that I would also like to show the date&time + X days. For example, add 5 days and I want it to show 2021-12-25 18:00:55. Is there a good solution for a case like this?
*I work with Presto
Remove T and X, cast to timestamp and add interval 5 day:
select cast(regexp_replace('2021-12-20T18:00:55.126X','^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d{3})X$','$1 $2') as timestamp) + interval '5' day
Result:
2021-12-25 18:00:55.126
And if you want it as string without milliseconds you can format it:
select format_datetime(( cast(regexp_replace('2021-12-20T18:00:55.126X','^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d{3})X$','$1 $2') as timestamp) + interval '5' day),'yyyy-MM-dd HH:mm:ss')
Result:
2021-12-25 18:00:55
you can remove X from the string and use from_iso8601_timestamp to parse the timestamp:
select from_iso8601_timestamp(replace('2021-12-20T18:00:55.126X', 'X', ''))
To add days you can use either interval or date_add:
select now() + interval '1' day, date_add('day', 1, now())
i have the following problem,
i am trying to find the difference of two dates in hours and minutes, for example
select to_date('13.05.2021 09:30','DD.MM.YYYY HH24:MI')
- to_date('13.05.2021 08:15','DD.MM.YYYY HH24:MI') from dual;
obiously it returns the difference in days, so the output will be 0,
i am expecting somthing like 01:15
Depending on the data type you need for your result...
If an interval day to second will work, then you can do this:
select (date2 - date1) * interval '1' day from dual;
For example:
select (to_date('13.05.2021 09:30','DD.MM.YYYY HH24:MI')
- to_date('13.05.2021 08:15','DD.MM.YYYY HH24:MI'))
* interval '1' day as diff
from dual;
DIFF
-------------------
+00 01:15:00.000000
Give this a try; if you need a different data type for the result, let us know. Note that, stupidly, Oracle doesn't support aggregate functions for intervals; so if you need a sum of such differences, you should apply the aggregation first, and only use this trick to convert to an interval as the last step.
In addition to #mathguy answer in case you need a specific output format:
TO_CHAR does not work with INTERVAL values. Either use EXTRACT
EXTRACT(HOUR FROM ((date2 - date1) DAY TO SECOND)) ||':'|| EXTRACT(MINUTE FROM ((date2 - date1) DAY TO SECOND))
or REGEXP_SUBSTR
REGEXP_SUBSTR((date2 - date1) DAY TO SECOND, ' \d{2}:\d{2}')
You may need some fine-tuning for proper output.
Is there a way to cast a date to timestamp. For an example if I had a date like 2012/05/01, how can I convert it to a timestamp like 2012-05-01 00:00:01
You can convert it to a timestamp with this code:
SELECT current_date::timestamp
It will directly assign the time 00:00:00 to current_date.
You can cast the date column to text and then concatenate with the time portion of the timestamp. In the query below I create a timestamp from the current date.
select (cast(current_date as text) || ' 00:00:01'):: timestamp
from yourTable;
Or if we already have a date type, we can simply add on the time component:
select current_date + '00:00:01'::time
Output:
11.07.2017 00:00:01
Demo
Update:
If you just want the difference in months between two dates you can use the following:
DATE_PART('month', AGE(end_date, start_date))
Of course, there is no time component involved here, but assuming you were planning to assign the dummy 00:00:01 to both timestamps, the result would not change.
select cast(current_date as timestamp) + interval '1 second'
Close to Standard SQL, only the interval syntax differs interval '1' second.
You can add time part to date
select current_date + '00:00:01'::time
One of my column needs to be transformed into a date field. It contains a value that gives the YYMM and it should be translated into the last day of that month:
For example, 1312 should become 12/31/2013.
I have tried various last_day, to_char functions but not able to convert 1312 in a date format. Please help !!
Netezza is based on Postgres, so maybe the Postgres method will work. Here is Postgres code that works (see here):
select to_date('1312'||'01', 'YYMMDD') + interval '1 month' - interval '1 day'
I would first convert the number to a date, then add 1 month and subtract 1 day.
select add_months(to_date(1312, 'yymm'), 1) - 1 as the_date