How to get current month and Last month in Redshift - amazon-redshift

I am wondering if I can get last month and current month in Redshift? I looked at functions at http://docs.aws.amazon.com/redshift/latest/dg/r_CURRENT_DATE_TIME_functions.html
For current month, I guess I can do the following:
LEFT(CURRENT_DATE, 7)
For previous month, I came up with the following:
LEFT(ADD_MONTHS(CURRENT_DATE, -1), 7)
But I am wondering if there is a simpler way of doing these?

SELECT EXTRACT(month FROM CURRENT_DATE);
SELECT EXTRACT(month FROM CURRENT_DATE - '1 month'::interval);

Related

Finding the last date of the previous quarter from current date in PostgreSQL

For example: If my current date is 2022-07-21, my query should give me 2022-06-30.
I could easily do it in IBM DB2 but struggling in postgresql.
You can truncate the current date to its quarter, then remove 1 day from that (and potentially cast back to date):
-- You really only need the last column, the other two just show the different steps in the process
SELECT DATE_TRUNC('quarter', CURRENT_DATE)
, DATE_TRUNC('quarter', CURRENT_DATE) - '1 day'::INTERVAL
, (DATE_TRUNC('quarter', CURRENT_DATE) - '1 day'::INTERVAL)::DATE
outputs
+---------------------------------+---------------------------------+----------+
|date_trunc |?column? |date |
+---------------------------------+---------------------------------+----------+
|2022-07-01 00:00:00.000000 +00:00|2022-06-30 00:00:00.000000 +00:00|2022-06-30|
+---------------------------------+---------------------------------+----------+

Subtract month from date column

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

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

SSRS 2008 R2 Change First Day of the Week to Monday

This report aggregates data from the top by week into days using a drill down. Now, I'm just extracting the date parts from the date so I can group on the tables, and it works beautifully however, the first day of the week is set to 7, or sunday and I need it to start at 1, or monday. The obvious solution I've read is to SET DATEFIRST 1 - incidentally, when I try to use SET, I get an error message saying that it is not supported
I've read you can change the global settings of the report to do this - but I have no clue where and would prefer to be able to do it locally in this query if possible. Also, I've tinkered around with ##DATEFIRST but all it does, of course, is report that the first day of the week is 7.
SELECT DATENAME(week, CAST(Date AS date)) AS week, DATENAME(dw, CAST(Date AS date)) AS day, CONVERT(date, Date, 103) AS date, FROM_NUMBER, DURATION,
TYPE_OF_CALL, ID, DATEPART(dw, CAST(Date - 1 AS date)) AS dayCode
FROM CallCenterStatsCDR
WHERE (Date BETWEEN #StartDate AND #EndDate)
GROUP BY DATENAME(week, CAST(Date AS date)), CONVERT(date, Date, 103), DATENAME(dw, CAST(Date AS date)), FROM_NUMBER, DURATION, TYPE_OF_CALL, ID,
DATEPART(dw, CAST(Date - 1 AS date))
ORDER BY Date DESC
The datefirst function will impact all of the processes running on the server. Could you DateAdd(dd,1,YourDate) all of your date fields in the query to get the same result?

How to get the no of Days from the difference of two dates in PostgreSQL?

select extract(day from age('2013-04-06','2013-04-04'));`
gives me the no of days ! i.e.: 2 days
but it failed when I have a differnt month:
select extract(day from age('2013-05-02','2013-04-01'));
So what I need is to get the no of days as 32 days
Subtraction seems more intuitive.
select '2013-05-02'::date - '2013-04-01'::date
Run this query to see why the result is 31 instead of 32
with dates as (
select generate_series('2013-04-01'::date, '2013-05-02'::date, '1 day')::date as end_date,
'2013-04-01'::date as start_date
)
select end_date, start_date, end_date - start_date as difference
from dates
order by end_date
The difference between today and today is zero days. Whether that matters is application-dependent. You can just add 1 if you need to.