Problem loading an "interval" from a CSV file - postgresql

I am trying to create a table using a csv file that is very big. Amongst the data, I have a column named 'bouwjaar' which means construction year and I have selected 'date' as a date type. I would receive an error, therefore I changed the date type into an interval but it again won't work. It gives me the following error. What should I select as a date type?
ERROR: interval field value out of range: "1971-1980"
CONTEXT: COPY fundadata, line 24, column bouwjaar : "1971-1980"

An interval in PostgreSQL is not something with a starting point and and end point, but a duration like "9 years".
A more appropriate data type for that would be daterange, but the values would have to look like [1971-01-01,1981-01-01). You either have to pre-process the file before loading, or you have to load the data into a text column and post-process it.

Related

got the following error while declaring the timestamp sa data type in postgresql

My time stamp value format is like 20200203160857. I am declaring my variable time stamp as TIMESTAMP.
time_stamp TIMESTAMP NULL,
and I am copying a csv file in that the time stamp value is as shown above. While inserting i got the following error.
date/time field value out of range: "20200203160857"\n HINT: Perhaps you need a different "datestyle" setting.\n
use to_timestamp() function like below to manage your format of timestamp
to_timestamp('20200203160857', 'YYYYMMDDHH24MISS')

PostgreSQL, trying to copy column of dates from csv file to table's column

I have a PostgreSQL table that contains an empty column of type 'date'
I'm trying to copy date values from a CSV file.
But It raises this:
COPY books (publication_date) FROM 'path/to/file/pub.csv' CSV;
ERROR: date/time field value out of range: "11/31/2000"
This value is at index 8178 of the CSV, so it's not the entire file that's faulty.
I don't understand why, as the date seems perfectly fine.
So, how can I fix this or make Postgres ignore the faulty dates?
ERROR: date/time field value out of range: "11/31/2000"
I don't understand why, as the date seems perfectly fine.
Well, november has only 30 days, so the date is indeed invalid.
You need to set the datestyle to the required format.
https://www.postgresql.org/docs/7.2/sql-set.html
https://www.postgresql.org/docs/9.1/runtime-config-client.html#GUC-DATESTYLE
SET datestyle = DMY;

postgreSQL increment number in output

I am extracting three values (server, region, max(date)) from my postgresql> But I want to extract an additional 4th field which should be the numerical addition of 1 to 3rd field. I am unable to use date add function as in the database date field is defined as an integer.
date type in DB
date|integer|not null
tried using cast and date add function
MAX(s.date)::date + cast('1 day' as interval)
Error Received
ERROR: cannot cast type integer to date
Required output
select server, region, max(alarm_date), next date from table .....
testserver, europe, 20190901, 20190902
testserver2, europe, 20191001, 20191002
next date value should be the addition to alarm_date
To convert an integer like 20190901 to a date, use something like
to_date(CAST(s.date AS text), 'YYYYMMDD')
It is a bad idea to store dates as integers like that. Using the date data type will prevent corrupted data from entering the database, and it will make all operations natural.
First solution that came to my mind:
select (20190901::varchar)::date + 1
Which output 2019-09-02 as type date.
Other solutions can be found here.

Date/time field out of range in Postgresql, hour field has more than 24 hours

I'm trying to import a .csv file into a Postgres Database. One of the fields is 'Simple throughput time', with the format HH:mm:ss. However, all of these values are greater than 24:00:00, which seems to be the highest value for the data type, therefore, when I try to import such file I get the value out of range error.
date/time field value out of range: "384:00:00"
How can I import it then? The file has close to 10.000 rows, so I don't think I can change the format in the file. Is there any other type of data type I can change the column to?
Thank you in advance
What you are trying to store is neither a time or a date but an interval of time.
Therefore, you should use the interval type.
https://www.postgresql.org/docs/current/datatype-datetime.html

parse date time from string in spotfire

one column in my csv file is a date that is read as a string and it follows this pattern : 2018-09-19 10:27:28.409Z. I am struggling to convert the column from string to date.
The conversion options in spotfire didn't allow me to change the column type. however, I found the solution, at the moment of importing the data set (file) you need to specify the type (date time) and magically spotfire manages the conversion.