can't import files csv in pgAdmin 4 - postgresql

i will import data csv to postgresql via pgAdmin 4. But, there are problem
ERROR: invalid input syntax for type integer: ""
CONTEXT: COPY films, line 1, column gross: ""
i understand about the error that is line 1 column gross there is null value and in some other columns there are also null values. My questions, how to import file csv but in the that file there is null value. I've been search in google but not found similar my case.
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4,
country varchar,
duration float4,
language varchar,
certification varchar,
gross int,
budget int
);
And i try in this code below, but failed
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4 null,
country varchar null,
duration float4 null,
language varchar null,
certification varchar null,
gross float4 null,
budget float4 null
);
error message in image
I've searched on google and on the stackoverflow forums. I hope that someone will help solve my problem

There is no difference between the two table definitions. A column accepts NULL by default.
The issue is not a NULL value but an empty string:
select ''::integer;
ERROR: invalid input syntax for type integer: ""
LINE 1: select ''::integer;
select null::integer;
int4
------
NULL
Create a staging table that has data type of varchar for the fields that are now integer. Load the data into that table. Then modify the empty string data that will be integer using something like:
update table set gross = nullif(trim(gross), '');
Then move the data to the production table.
This is not a pgAdmin4 issue it is a data issue. Working in psql because it is easier to follow:
CREATE TABLE public.films_text
(
id varchar,
title varchar,
release_year varchar,
country varchar,
duration varchar,
language varchar,
certification varchar,
gross varchar,
budget varchar
);
\copy films_text from '~/Downloads/films.csv' with csv
COPY 4968
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4,
country varchar,
duration float4,
language varchar,
certification varchar,
gross int,
budget int
);
-- Below done because of this value 12215500000 in budget column
alter table films alter COLUMN budget type int8;
INSERT INTO films
SELECT
id::int,
title,
nullif (trim(release_year), '')::real, country, nullif(trim(duration), '')::real,
LANGUAGE,
certification,
nullif (trim(gross), '')::float, nullif(trim(budget), '')::float
FROM
films_text;
INSERT 0 4968

It worked for me:
https://learnsql.com/blog/how-to-import-csv-to-postgresql/
a small workaround but it works
I created a table
I added headers to csv file
Right click on the newly created table-> Import/export data, select csv file to upload, go to tab2 - select Header and it should work

Related

Generate value from columns in Postgres

I would like to have a generated column, which value will be the concated string from two other values:
CREATE TABLE public.some_data (
user_name varchar NULL,
domain_name serial NOT NULL,
email GENERATED ALWAYS AS (user_name ||'#'||domain_name) stored
);
But that gives SQL Error [42601]: ERROR: syntax error at or near "ALWAYS"
You need to provide the data type for the column as #Belayer commented.
And then you need to explicitly cast domain_name as text (or some varchar). Otherwise you'll get an error that the expression isn't immutable as #nbk commented. serial is translated to be basically an integer and for whatever reason implicit casts of an integer in concatenations are considered not immutable by the engine. We had that just recently here.
So overall, using the given types for the columns, you want something like:
CREATE TABLE public.some_data
(user_name varchar NULL,
domain_name serial NOT NULL,
email text GENERATED ALWAYS AS (user_name || '#' || domain_name::text) STORED);
But it's a little weird that a domain name is a serial? Shouldn't that be a text or similar? Then you wouldn't need the cast of course.
You need to create an IMMUTABLE function to achieve the generate column, for example:
CREATE OR REPLACE FUNCTION generate_email_concat(varchar,int) returns text as
$$
select $1 ||'#'||$2::text;
$$
LANGUAGE SQL IMMUTABLE ;
CREATE TABLE public.some_data (
user_name varchar NULL,
domain_name serial NOT NULL,
email text GENERATED ALWAYS AS (generate_email_concat(user_name,domain_name)) stored
);
INSERT into some_data(user_name) values ('hello');
You try to concatenate varchar and integer. You have to cast domain_name. This works for me
CREATE TABLE public.some_data (
user_name varchar NULL,
domain_name serial NOT NULL,
email varchar GENERATED ALWAYS AS (CASE WHEN user_name IS NULL THEN 'noname'||'#'||domain_name::text ELSE user_name ||'#'||domain_name::text END) STORED
);

how to extecute multiple select queries with different returning data in postgresql?

I'm using postgreql and i have two tables and i want to execute two select queries on them. the data that returning from each select are varying!
the data that returns from first table is:
id integer, first_name varchar, last_name varchar, email varchar, company varchar,positions varchar,address varchar,phone varchar
and the second table return:
group_contact_id integer, contact_id integer, group_id integer
i want to do it in a function like this:
create function findcontactbyid(id integer) returns table (id integer, first_name varchar, last_name varchar, email varchar, company varchar,positions varchar,address varchar,phone varchar, group_contact_id integer, contact_id integer, group_id integer) as $$
select * from cms_contact where id = $1
UNION ALL
select * from cms_groups_contacts where contact_id = $1
$$ language 'sql'
but i get errors
$1 mentions to (id integer) and it exists in both tables
I rather wonder about your function! What do you really need here.
Comeback to the issues:
I think it will raise an error because PostgreSQL have an ambiguous between id (INPUT), id (RETURN) and id of tables in query. So, if you can, you can named it different to each other and then check it again.
The second think, I have some worry about the UNION QUERY. In my knowledge, when you want to UNION 2 query to each other, the result of both query must be the same column and the same type of column.
So, I think after you fix the first issue, the function is also can not run. But you can try first.

using aggregation function while creating function in psql

Hi guys i have little problem, i have tables like those
CREATE TABLE client(
regon VARCHAR NOT NULL,
title VARCHAR,
phone VARCHAR,
PRIMARY KEY(regon));
CREATE TABLE commodity(
id_com INT NOT NULL,
title VARCHAR,
PRIMARY KEY(id_com));
CREATE TABLE supply(
regon VARCHAR NOT NULL REFERENCES klient(regon),
id_supply INT NOT NULL,
id_com INT NOT NULLREFERENCES commodity(id_com),
quantity INT,
price DEC(5,2),
PRIMARY KEY(regon, id_supply, id_com));
and i have to create function which would returns value of all supplies (qantity*price)
and i made function like this:
CREATE OR REPLACE FUNCTION value1(out id int, out war double precision)as $$
select (quantity*price) as value from supply;
$$
language 'plpgsql';
but it only shows the first supply with id of first commodity with its value but not all commodities
maybe
you know how to do this?
thanks
Change this select (quantity*price) as value from supply;
to this:
select sum(quantity*price) from supply
group by id_com
order by id_com
I had to do query like this:
select sum(quantity*price) from supply
group by id_com,quantity,price
order by id_com
and it is also showing me id_com double times like i have two commodities with id 1 and one is 400 and second is 30, and i think i should maybe sum this up

Varchar to Numeric Conversion

I have loaded the excel data into a table using SSIS.
Table Structure :
Monthly_Budget
SEQUENCE INT IDENTITY,
TRANSACTION_DATE VARCHAR(100),
TRANSACTION_REMARKS VARCHAR(1000),
WITHDRAWL_AMOUNT VARCHAR(100),
DEPOSIT_AMOUNT VARCHAR(100),
BALANCE_AMOUNT VARCHAR(100)
Values in WITHDRAWL_AMOUNT Column:
7,987.00
1,500.00
7,000.00
50.00
NULL
253.00
4,700.00
2,000.00
148.00
2,000.00
64.00
1,081.00
2,000.00
NULL
NULL
7,000.00
Now, I am trying to run a query to get the summation of values under WITHDRWAL_AMOUNT but I am getting an error :
Error converting data type varchar to numeric.
My Query :
SELECT SUM(CAST(ISNULL(LTRIM(RTRIM(WITHDRAWL_AMOUNT)),0) AS NUMERIC(6,2))) AS NUM FROM MONTHLY_BUDGET
Try converting them like this:
select SUM(CAST(ltrim(rtrim(replace(WITHDRAWL_AMOUNT, ',', ''))) as numeric(6, 2)) )
It is much, much preferable to store the values in the proper types that you want. I can, however, understand putting external data into a staging table and then using logic such as the above to load the data into the final table.

PostgreSQL create table syntax

I'm more a mysql person, but I have to do a db in pg and the following CREATE TABLE keeps generating syntax errors... I just get an error: ERROR: syntax error at or near "(" and error: ERROR: syntax error at or near ")" Googling around didn't give me much help... I'm sure that I'm doing something mysql-esque and that's causing problems... (Note: I did already create the mfseq successfully...)
CREATE TABLE master_file (
mfid INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('mfseq'),
prefix VARCHAR(4),
fname VARCHAR(30) NOT NULL,
lname VARCHAR(80) NOT NULL,
MI varchar(1) NULL,
address1 VARCHAR(200) NOT NULL,
address2 VARCHAR(200),
city VARCHAR(28),
state VARCHAR(2),
zip INT(5),
zip_plus4 INT(4),
mrn VARCHAR(30),
aID INT,
iID INT,
gID VARCHAR(1),
pphone VARCHAR(10);
);
Maybe int -> integer and without size (or numeric) and delete the delimiter at pphone field.
It should not be a semi-colon here: pphone VARCHAR(10);