Subtract month from date column - date

I would like to subtract one month from a date column in Amazon Redshift (a fork of PostgreSQL 8.0.2).
So for each date column in a table, it will add another column date_minus_a_month.
I tried this code
Select date,date::date -interval '1 month'
from table
and received an error:
Interval values with month or year parts are not supported.
Does anyone have a solution for this?

You can use datesub, although I just use dateadd for everything and use negative numbers.
eg
SELECT getdate() as right_now, dateadd(month, -1, getdate()) as last_month
Docs: https://docs.aws.amazon.com/redshift/latest/dg/r_DATEADD_function.html

Related

How to select data between two dates using only the start date?

I have problem select data between two dates if the only start_date is available.
The example I want to see is what discount_nr was active between 2020-07-01 and 2020-07-15 or only one day 2020-07-14. I tried different solutions, date range, generate series, and so on, but was still not able to get it to work.
Table only have start dates, no end dates
Example:
discount_nr, start_date
1, 2020-06-30
2, 2020-07-03
3, 2020-07-10
4, 2020-07-15
You can get the end dates by looking at the start date of the next row. This is done with lead. lead(start_date) over(order by start_date asc) will get you the start_date of the next row. If we take 1 day from that we'll get the inclusive end date.
Rather than separate start/end columns, a single daterange column is easier to work with. You can use that as a CTE or create a view.
create view discount_durations as
select
id,
daterange(
start_date,
lead(start_date) over(order by start_date asc)
) as duration
from discounts
Now querying it is easy using range operators. #> to check if the range contains a date.
select *
from discount_durations
where duration #> '2020-07-14'::date
And use && to see if they have any overlap.
select *
from discount_durations
where duration && daterange('2020-07-01', '2020-07-15');
Demonstration

How to subtract a number of days in one column from CURRENT_DATE in PostgreSQL

I have a column called number_of_days_since_event and I want to change it to data_of_last_event, the way I can do that is subtracting today's date from the number of days in the number_of_days_since_event column. But I do not know how to subtract days in a column.
This question answers the problem when you know the number of days in advance, i.e., if I would like to subtract 10 days from today it would be:
SELECT CURRENT_DATE - INTERVAL '10 days';
However, I would like to do something like:
SELECT CURRENT_DATE - INTERVAL "myTable.number_of_days_since_event" 'days'
FROM myTable;
But this does not work leading to the error message: syntax error at or near "'day'"
The following using concat solves my problem:
SELECT CURRENT_DATE - concat(myTable.number_of_days_since_event::text,' day')::interval
FROM myTable;
If you are happy with a date result, you could use
SELECT current_date - number_of_days_since_event::integer
FROM mytable;

How to get records from table which has timestamp starting from particular date of previous month to particular date of this month in postgres?

I have a customer_visit table, my requirement is to get the records of previous month starting from 3rd date to current month 2nd date and which contains flag 'Y'. How can I achieve this in Postgres. Following is the sample of my table. Any help is appreciated.
Use make_date() function from postgres to construct the date and query on it.
select * from customer_visit
where created_time > make_date(2018, date_part('month', now())::int - 1, 03)
and created_time <= make_date(2018, date_part('month', now())::int, 02)
and flag = 'Y';
You can use date_part('year', timestamp) to extract year also from current date.

DB2- select rows with date_column of last month

I need to select count(field) from table where the date_column is within the last month, i.e. from the first of the last month till the first of this month. As this query will be automated it will need to refer to the last month of any given day.
Also the date_column has a datatype of timestamp
Thanks in advance!
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS FROM sysibm.sysdummy1;
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS
- 1 MONTH
FROM sysibm.sysdummy1;
Managed to find these queries, to find the first of this month and last month

PostgreSQL: exists robust third party date-math functions to augment the built-in date operators?

I'm porting some T-SQL stored procs to PL/pgSql and, being very new to PostgreSQL, don't know what helpful utility functions might be available in the pg community. Is there a set of robust date-math functions that "nearly everybody uses" out there somewhere? I don't want to quickly cobble together some date-math functions if there's already a great package out there.
The PostgreSQL date math operators with "natural language" string literal arguments are user-friendly if you're typing a query and you happen to know the interval:
select now() - interval '1 day'
but if the interval 1 is the result of a calculation involving nested date-math function calls, these string literals are actually not very user-friendly at all, and it would easier to work with a date_add function:
select dateadd(d, {calculation that returns the interval}, now() )
Thanks
Let me give you an example. I want to subtract from an arbitrary date the number of months that have elapsed since 1/1/1970, and then add that number of months to 1/1/1970 to return the first day of the month in which the arbitrary date falls
select (date_trunc('month', '2013-01-30'::date))::date
Or add a month to the first day of this month to get the first day of the next month, then subtract one day to get the last day of this month
select date_trunc('month', '2013-01-30'::date + 1 * interval '1 month')::date - 1
Notice in the above example you can add any number of months by multiplying the interval '1 month' by an integer. You can do that with any interval without manipulating the string '1 month'. So to add or subtract any interval you just:
select current_date + 5 * interval '1 month'
No need for messy string manipulations. You can multiply by fractions also:
select current_timestamp + 3.5 * interval '1 minute'
To add or subtract days to a date type you use an integer:
select current_date + 10
The "natural language" strings you're talking about are interval literals. Intervals can also be obtained by using date arithmetic.
Surely dateadd can be quite simply emulated in Postgresql as follows:
select d + ({calculation the returns the interval}::text || ' day')::interval
Substitute "month" or "hours" etc as appropriate.
In PostgreSQL, you simply add and subtract interval values to datetime
values:
'2001-06-27 14:43:21'::TIMESTAMP - '00:10:00'::INTERVAL = '2001-06-27 14:33:21'::TIMESTAMP
'2001-06-27 14:43:21'::TIMESTAMP- '2001-06-27 14:33:21'::TIMESTAMP = '00:10:00'::INTERVAL
For more information, see "Functions and Operators" in the PostgreSQL
online docs.
To compute the first day of the month of a date: date_trunc('month', date)
First day of the next month: date_trunc('month', date) + '1 month'::INTERVAL
Add three months to the first day of the month of this date: date_trunc('month', date) + 3*('1 month'::INTERVAL)
The interval is a data type, not a string, and you can do computations with its values.