I have a requirement to create a column name in snowflake such that it displays the current month end date. It should change dynamically every month.
Expected table structure.
Tablename:- sample
Column names :-
1.Name - Varchar field
2.2023-01-31 (Should be Current monthend) - Integer field.
When I do a select * from sample NEXT month , the column names should be as below :
| Name | 2023-02-29 |
|---|---|
| A1 | 15876 |
| B3 | 8976 |
I have tried something like this using identifier, and it is throwing the below error in the create table statement.
"Unexpected '('.syntax error line 5 at position 0 unexpected ')'.
Set NEW123=(to_varchar(Current_Date(),'YYYY-MM-DD');
CREATE TABLE SAMPLE
(
IDENTIFIER($NEW123) INTEGER
);
so the SQL you wish the results to be would be the equivalent of:
the table name needs to be in quotes because
sampleis a reserved word.using LAST_DAY we can get the month end:
so now we just need to build the SQL we want:
lovely, lovely, now lets run that via EXECUTE IMMEDIATE