Skip to content
Advertisement

Create a Month function

I would like to run a query that runs monthly but it will check daily if the timestamp from the records is from the previous month. If yes then it will run the query and append to the previous monthly data.

So, I want to create a simple function to return TRUE if the RecordTimestamp is from the previous month. Something like this:

CREATE TEMP FUNCTION isMonth(recordTimestamp TIMESTAMP) AS (  xxxxxxx );

What would be right syntax for this?

Advertisement

Answer

return TRUE if the RecordTimestamp is from the previous month

Use below

create temp function isPrevMonth(ts timestamp) as (
  date_trunc(date(ts), month) = date_trunc(current_date() - interval 1 month, month)
);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement