

Syntactically, this function takes no argument. Before going to date and time functions, let us learn what each of these data types represents. The current session started refers to the date and time during a transaction in the current transaction. Redshift’s date and time functions help the customers to transform or manipulate data that is stored in the Redshift table with the data type as DATE, TIME, TIMETZ, TIMESTAMP, and TIMESTAMPTZ.

create or replace procedure public.rebuild_test_table() TRUNC returns the same data type as the first input argument. The Redshift SYSDATE Function returns the current date and time in the current session time zone (in UTC by default). Back to the original request, please also of note that using an entirely numeric column name will require the use of double quotes every time you reference the column and mistakes by users regarding this will assuredly occur and cause their own confusion.Ĭaveats and recommendations aside, here is a very quick implementation that should be close to what you specifically asked about. For example, your dynamically named column could be replaced with a column named "as_of_month" where you simply include which month you care about in the WHERE clause of your query - for example: select * from test where as_of_month = '202303' Not knowing your exact use case, perhaps that would be: create table temp as select to_char(year_month,'YYYYMM') as as_of_month, count(psid) from tb_app_opened_globoplay_redu group by to_char(year_month,'YYYYMM') where you can then query for your character formatted year month of 13 months ago. Theres also the datetrunc function which will truncate everything after a given datepart. This is less true if you are adding a new column for each month, but I would still then recommend a different architecture.
#Redshift datetrunc code#
This is especially true if you plan to replace the previous name with a new name because the downstream code will fail if the column name doesn't exist (yet).

For example downstream code generally needs to know what columns exist and what their names are.
DATETRUNC supports the following values for datepart: DAY WEEK WEEK (#Redshift datetrunc how to#
I have provided an example of how to do that below, but I would caution that this is a problematic solution. DATETRUNC Description Truncates the date to the specified granularity. To make a dynamically named column name you will need to use dynamic SQL - a SQL statement defined as a character string in code and then executed.
