You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/sql/copy.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,16 @@ The command starts with the keyword `COPY`, followed by the name of the table yo
21
21
`TO` specifies the file path and name to save the exported
22
22
data (`/xxx/xxx/output.parquet` in this case).
23
23
24
+
For example, to export data to CSV with custom timestamp and date formats:
25
+
26
+
```sql
27
+
COPY tbl TO '/path/to/file.csv' WITH (
28
+
FORMAT ='csv',
29
+
TIMESTAMP_FORMAT ='%Y/%m/%d %H:%M:%S',
30
+
DATE_FORMAT ='%Y-%m-%d'
31
+
);
32
+
```
33
+
24
34
#### `WITH` Option
25
35
26
36
`WITH` adds options such as the file `FORMAT` which specifies the format of the exported file. In this example, the format is Parquet; it is a columnar storage format used for big data processing. Parquet efficiently compresses and encodes columnar data for big data analytics.
@@ -29,6 +39,9 @@ data (`/xxx/xxx/output.parquet` in this case).
29
39
|---|---|---|
30
40
|`FORMAT`| Target file(s) format, e.g., JSON, CSV, Parquet |**Required**|
31
41
|`START_TIME`/`END_TIME`| The time range within which data should be exported. `START_TIME` is inclusive and `END_TIME` is exclusive. | Optional |
42
+
|`TIMESTAMP_FORMAT`| Custom format for timestamp columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%Y-%m-%d %H:%M:%S'`). Only supported for CSV format. | Optional |
43
+
|`DATE_FORMAT`| Custom format for date columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%Y-%m-%d'`). Only supported for CSV format. | Optional |
44
+
|`TIME_FORMAT`| Custom format for time columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%H:%M:%S'`). Only supported for CSV format. | Optional |
32
45
33
46
#### `CONNECTION` Option
34
47
@@ -145,13 +158,26 @@ COPY (<QUERY>) TO '<PATH>' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' });
145
158
|`QUERY`| The SQL SELECT statement to execute |**Required**|
146
159
|`PATH`| The file path where the output will be written |**Required**|
147
160
|`FORMAT`| The output file format: 'CSV', 'JSON', or 'PARQUET' |**Required**|
161
+
|`TIMESTAMP_FORMAT`| Custom format for timestamp columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
162
+
|`DATE_FORMAT`| Custom format for date columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
163
+
|`TIME_FORMAT`| Custom format for time columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
148
164
149
165
For example, the following statement exports query results to a CSV file:
150
166
151
167
```sql
152
168
COPY (SELECT*FROM tbl WHERE host ='host1') TO '/path/to/file.csv' WITH (FORMAT ='csv');
153
169
```
154
170
171
+
You can also specify custom date and time formats when exporting to CSV:
172
+
173
+
```sql
174
+
COPY (SELECT*FROM tbl WHERE host ='host1') TO '/path/to/file.csv' WITH (
175
+
FORMAT ='csv',
176
+
TIMESTAMP_FORMAT ='%m-%d-%Y %H:%M:%S',
177
+
DATE_FORMAT ='%Y/%m/%d'
178
+
);
179
+
```
180
+
155
181
## COPY DATABASE
156
182
157
183
Beside copying specific table to/from some path, `COPY` statement can also be used to copy whole database to/from some path. The syntax for copying databases is:
0 commit comments