Filtering based on end date being within the next 30 days [duplicate] - sql

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

Transform timestamp to date&time

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())

Subtract time using Oracle PL/SQL

I'm creating a report using BI Publisher. Now, I want is to subtract 4 hours in the date. For example, the date I get in the oracle database is below,
2019-09-23T10:09:34.054+00:00
Now I want it to return in report using sql is,
2019-09-23T06:09:34.054+00:00
How can I do that?
Thanks!
Use INTERVAL?
SELECT ts - INTERVAL '4' HOUR
FROM yourTable;
Demo
If your source data is actually text, and not a bona fide timestamp column, then you may use TO_TIMESTAMP_TZ to first do a conversion:
SELECT
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') AS ts_original,
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') - INTERVAL '4' HOUR AS ts_offset
FROM yourTable;
You just need to subtract a fraction of time. Like this for 4 hours ago:
select sysdate-1/6 from dual;

Cast date to timestamp in pgSQL

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

how do I convert mmyy to last day of month in netezza

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

Add Day to Timestamp

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