Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to import large csv file its around 1GB of size using copy command. But the condition is if lets see Column A1 datatype is Numeric and the data of the csv file is also of type numeric but some of the values are in alpha numeric. so its not importing . What i want is set the non-numeric value of the colum A1 to null and import the data. All the condition i want in the copy command . Sample of csv file.

<table border=true>
<tr>
<th >A1</th>
<th>A2</th>
<th>A3</th>
</tr>
<tr>
<td>90</td>
<td>91</td>
<td>92</td>
</tr><tr>
<td>A21</td>
<td>B23</td>
<td>C43</td>
</tr><tr>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>12ddwd3</td>
<td>1yhh53</td>
<td>1dfsf754</td>
</tr>
<tr>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
</table>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

you cant conditionnaly COPY table from file. what you can do instead:

create table t (a1 text, a2 text, a3 text);
COPY t from file;
update t set a1 = null where a1 !~ '^d{1,}$';
alter table t alter column a1 type int using a1::int;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...