Import date dd/mm from txt to table with SQL - sql

I'm a total beginner and already searched all over the place, so please bear with me.
I have a txt file with this kind of data (DD/MM) and ; as delimiters:
01/10;10/06;15/11;10/07
01/10;10/06;15/11;10/07
01/11;20/06;10/11;30/07
01/11;20/06;10/11;30/07
10/11;20/06;20/01;30/07
01/10;01/06;15/11;30/06
Firstly, I set datestyle to European;
So I have DateStyle - "ISO, DMY".
After, I tried to import this data into some of the columns of the pheno table (see code below), using postgresql:
COPY pheno(planting_onset, harvesting_onset, planting_end, harvesting_end)
FROM '/home/user/Documents/worldcrops/algeria_times.txt' DELIMITERS ';';
And gave the following error:
ERROR: invalid input syntax for type date: "01/10"
CONTEXT: COPY pheno, line 1, column planting_onset: "01/10"
********** Error **********
ERROR: invalid input syntax for type date: "01/10"
SQL state: 22007
Context: COPY pheno, line 1, column planting_onset: "01/10"
Questions: How do I copy this data type DD/MM into a table which columns have date as "data type"? Should I change the columns "data type"?
Thanks in advance.

It's expecting DMY but you're only giving it days and months. This is kind of hacky but i think it should work:
ALTER TABLE pheno
ADD planting_onset_temp VARCHAR(16),
harvesting_onset_temp VARCHAR(16),
planting_end_temp VARCHAR(16),
harvesting_end_temp VARCHAR(16);
COPY pheno(planting_onset_temp, harvesting_onset_temp, planting_end_temp, harvesting_end_temp) FROM '/home/user/Documents/worldcrops/algeria_times.txt' DELIMITERS ';';
UPDATE pheno
SET planting_onset = CONCAT(planting_onset_temp, '/2016'),
harvesting_onset = CONCAT(harvesting_onset_temp, '/2016'),
planting_end = CONCAT(planting_end_temp, '/2016'),
harvesting_end = CONCAT(harvesting_end_temp, '/2016');
ALTER TABLE pheno DROP COLUMN planting_onset_temp, harvesting_onset_temp, planting_end_temp, harvesting_end_temp;
Replace '/2016' with whatever year is relevant.

Related

POSTGRESQL invalid input syntax for type integer

I am a novice user of PostgresQL. I found out in Internet interesting DB, which consists of 3 tables. Every of this table is in CSV format. However I could smoothly copy the first one to my sql database. Right now I have the following problem:
ERROR: invalid input syntax for type integer: "43950100,43950010,TUR,GUNES Senol (TUR),S,1,"RUSTU ",GK,"
CONTEXT: COPY worldcupsplayers, line 25645, column roundid: "43950100,43950010,TUR,GUNES Senol (TUR),S,1,"RUSTU ",GK,"
Every row in a table has the same structure, as follows:
43950100,43950010,TUR,GUNES Senol (TUR),S,1,"RUSTU ",GK,
I am wondering why in such row I have a problem (line 25645), while it looks as other ones.
I am trying to copy it with this command:
COPY worldcupsplayers(RoundID, MatchID, team_initials, coach_name, line_up, shirt_number, player_name, position, event)
FROM 'C:\Users\Public\worldcup\worldcupplayers.csv'
DELIMITER ','
CSV HEADER;
[table structure]
UPDATE: rows above and below
25643 - 43950100,43950008,SVN,KATANEC Srecko (SVN),N,23,BULAJIC,,
25644 - 43950100,43950010,BRA,SCOLARI Luiz Felipe (BRA),S,1,MARCOS,GK,
25645 - 43950100,43950010,TUR,GUNES Senol (TUR),S,1,"RUSTU ",GK,
25646 - 43950100,43950010,BRA,SCOLARI Luiz Felipe (BRA),S,2,CAFU,C,
As u may see they are all the same, with the commas every row has the same structure.

How to determine the column with multiple items of data has one specified value in BigQuery?

This is my table:
I want to change the TargetCondition column to 'TRUE' when the ICD9CODE column contains a particular range (like between 250 and 250.93).
Please help me do it. Thanks
This is the code here:
update demo.fea_02 set TargetCondition = TRUE where ICD9Code like between '%250%' and '%250.93%'
got the error:
Syntax error: Unexpected keyword BETWEEN at [6:17]
This answers the original version of the question.
You would use update:
update mytable
set targetcondition = 'TRUE'
where icd9code like '%value%';

REPLACE statement produces "Data truncation" error. Please advise why and how to correct it

UPDATE ASSIGNMENTS SET CBTURL = REPLACE(CBTURL, 'http://172.21.130.19/', 'https://testlpsweb.corp.mbll.ca/Content/')
The above statement produces "Data truncation" error. Please advise why and how to correct it.
Error starting at line : 1 in command - UPDATE ASSIGNMENTS SET CBTURL = REPLACE(CBTURL, 'http://172.21.130.19/', 'https://testlpsweb.corp.mbll.ca/Content/') Error at Command Line : 1 Column : 1 Error report - SQL Error: Data truncation
I'm guessing that CBTURL column length is to small for resulting string of replace. Could you try to alter column to have larger lenght.
Try this query to see maximum resulst string lenght:
Select Max(Len(REPLACE(CBTURL, 'http://172.21.130.19/', 'https://testlpsweb.corp.mbll.ca/Content/'))) from tablename ....

Update Field Geometry Polygon PostGIS PostgreSQL

I am having some trouble trying to update a field.
I created a column as text and stored many rows with (lon,lat). Now I have created one more field as Geometry(Polygon) and I am trying to update it unsuccessfully.
The data is stored like this:
POLYGON ((-16.6318775869111 -52.5925428149806, -16.6346393504709 -52.572542814981
, -16.629462102066 -52.5525428149806, -16.6255191065928 -52.5455420519144
, -16.6055191065928 -52.5488828022871, -16.6021791014778 -52.552542814981
, -16.6108484688169 -52.5925428149806, -16.6255191065928 -52.599495926874
, -16.6318775869111 -52.5925428149806))
I am trying this command
UPDATE field_as_text
SET field_as_geomtry = SELECT ST_GeomFromText(SELECT field_as_text);
I am getting this error message
ERROR: syntax error at or near "select"
LINE 1: ... set field_as_geomtry = select (ST...
Could anyone enlighten me?
You need to start by reading the manual about the SQL UPDATE command.
Should look something like this:
UPDATE table_name
SET field_as_geomtry = ST_GeomFromText(field_as_text);

Firebird " Column does not belong to referenced table "

I am trying to create my first procedure on firebird 2.5 by using ibexpert gui.
The procedure will return 'PROCESS_DATE' which belongs to a specific 'PROCESS_ID'. I prepared following code:
begin
OUTPUT_DATE = (select PROCESS_DATE from PROCESSES
where PROCESS_ID = INPUT_ID);
suspend;
end
input parameter : 'INPUT_ID' --> type 'INTEGER'
output parameter : 'OUTPUT_DATE' --> type 'DATE'
But when I tried to compile it returns this error:
Column does not belong to referenced table.
Dynamic SQL Error.
SQL error code = -206.
Column unknown.
INPUT_ID.
At line 9, column 48.
I do not know how to deal with this error.
I tried to find solutions on other questions also the internet but i couldn't find a basic, understandable answer for beginners. thanks for helps.
Try this:
CREATE PROCEDURE MyP (INPUT_ID INTEGER)
RETURNS (OUTPUT_DATE DATE)
AS
BEGIN
FOR
SELECT PROCESS_DATE FROM PROCESSES
WHERE PROCESS_ID = :INPUT_ID
INTO :OUTPUT_DATE
DO
SUSPEND;
END
Always prepend parameter names with ":". The only place where ":" is not allowed is at the left side of "=" operator.