Friday, October 23, 2020

EXPORT SNOWFLAKE TABLE DATA TO CSV BY ADDING DOUBLE QUOTES

One has to use "FIELD_OPTIONALLY_ENCLOSED_BY" parameter while exporting the Snowflake table data to CSV by adding double quotes.

$ snowsql -a <Snowflake_Account_Name> -u <username>

Password: 

Ø $snowsql > use <Database_name>;

           +----------------------------------+                                            

            status                           |

            |----------------------------------|

            | Statement executed successfully. |

            +----------------------------------+

Ø $snowsql > use WAREHOUSE <Warehousename>;

            +----------------------------------+                                            

            status                           |

            |----------------------------------|

            | Statement executed successfully. |

            +----------------------------------+

Ø $snowsql > use schema <schema_name>;

          +----------------------------------+                                            

          status                           |

        |----------------------------------|

        | Statement executed successfully. |

         +----------------------------------+

Ø $snowsql > create or replace table test (C1 VARCHAR(20), C2 VARCHAR(20),C3 VARCHAR(20), C4 CHAR(1), C5 INTEGER, C6 NUMBER ,TS TIMESTAMP_LTZ);

           +----------------------------------+                                            

           status                           |

           |----------------------------------|

           | Table TEST successfully created. |

           +----------------------------------+

Ø insert into test values ('A','B,C,D','E','F',1,2, current_timestamp);

           +-------------------------+                                                     

            number of rows inserted |

            |-------------------------|

            |                       1 |

            +-------------------------+

Ø select * from test;

          +----+-------+----+----+----+----+-------------------------------+              

          C1 | C2    | C3 | C4 | C5 | C6 | TS                            |

          |----+-------+----+----+----+----+-------------------------------|

         | A  | B,C,D | E  | F  |  1 |  2 | 2020-10-22 04:22:11.843 +0000 |

         +----+-------+----+----+----+----+-------------------------------+


Use below Syntax to Unload the data from Snowflake to Internal Stage. 

Ø copy into @my_stage/test.csv from TEST FILE_FORMAT = (TYPE = CSV FIELD_OPTIONALLY_ENCLOSED_BY = '"' ESCAPE_UNENCLOSED_FIELD = NONE );

             +---------------+-------------+--------------+                                  

             rows_unloaded | input_bytes | output_bytes |

             |---------------+-------------+--------------|

             |             1 |          52 |           71 |

             +---------------+-------------+--------------+

Ø get @my_stage/test.csv file:///Users//Desktop/Snowflake/SourceDataFile/;

          +-----------------------+------+------------+---------+

          file                   | size | status     | message |

          |-----------------------+------+------------+---------|

          | test.csv_0_0_0.csv.gz |   71 | DOWNLOADED |         |

          +-----------------------+------+------------+---------+

Output in the test.csv file:

"A","B,C,D","E","F",1,2,"2020-10-22 04:22:11.843 Z"

 

 

 

 

 

2 comments:

  1. Thanks for you great article Needed to compose you a very little word to thank you .
    Read more here

    ReplyDelete