I'm inserting records read from a file in date /or time field in Postgres. Many of the date/or time fields are blank in file. I want null to be inserted for the corresponding table fields. Postgres table col for date /or time is defined as date/ or time respectively. Using the query as below. I'm getting error : psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type date: " " Any directions as to where lies the problem will be helpful.
INSERT INTO userbasic
(name, create_date, owner_id, adsp, special,
oper, revoke, grpacc, pwd_interval, pwd_date,
programmer, defgrp_id, lastjob_time, lastjob_date, install_data)
VALUES
(%s, NULLIF(%s,'')::date, %s, %s, %s,
%s, %s, %s, %s, NULLIF(%s,'')::date,
%s, %s,NULLIF(%s,'')::time, NULLIF(%s,'')::date, %s)
Expect the records with no value for date/ or time col to be inserted to table with NULL.
' ', more then one space. YourNULLIF(%s,'')is only going to catch true empty strings. Aprint(len(variable))will help verify this. You will need to normalize the data to empty strings, either on the Python side or the SQL side.