snowflake-cloud-data-platform

How to rename file in external stage in Snowflake?


I am trying to rename an external Stage file using Snowflake.

copy files into @targetstage/newfilename from @inputstage.

The result is NewfilenameCurrentFilename. I want only new file name. Can anybody please suggest a workaround?


Solution

  • If renaming via SQL is mandatory and you're okay going through a table:

    1. Load the file into a temporary table.

    2. Use COPY INTO @targetstage/newfilename.csv to export.

    3. -- Step 1: Load to table

      COPY INTO my_temp_table FROM @inputstage;

      -- Step 2: Export with desired filename

      COPY INTO @targetstage/newfilename.csv FROM my_temp_table FILE_FORMAT = (TYPE = CSV);