0

This might be impossible but I was wondering if someone more experienced knew if this is possible to do in postgresql.

I have a column in my create statement

CREATE table IF NOT EXISTS (other cols, some_date DATE, other cols);

When I extract the json object from the API, there might actually be an empty string '' instead of a empty cell. Which of course gives me the following error psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type date: ""

The solution would simply to change the constraint to VARCHAR, but i was wondering if there was some way in the CREATE TABLE or INSERT statements to say the following pseudo code: if empty string insert NULL.

1
  • If all else fails, you could import to a staging table then insert from there with a case on the insert Commented May 22, 2019 at 13:41

2 Answers 2

3

Use NULLIF in your INSERT statement:

INSERT INTO your_table (cols..., some_date) VALUES (..., NULLIF(your_input_field, ''))

If you want to insert NULL if the value in question is any of a number of values, it may be easiest to use a CASE statement:

INSERT INTO your_table (cols..., some_date)
VALUES (..., CASE WHEN your_input_field IN ('', '#', '-', '--', '??') THEN NULL ELSE your_input_field END)

Could do the same with an array as well, if that's easier:

INSERT INTO your_table (cols..., some_date)
VALUES (..., CASE WHEN your_input_field = ANY('{"",#,-,--,??}'::TEXT[]) THEN NULL ELSE your_input_field END)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! your_input_field should be replaced by some_date?
your_input_field is the JSON field value you want to insert in the some_date field in your table, which could be an empty string. Doing a NULLIF on the value will return NULL if the value of the first parameter is equal to the value of the second parameter. Let me know if I misunderstood your question.
Jackpot would be if I could insert a NULL for a list of strings like # - -- ?? etc... the so-called NITS (nothing interesting to say).
Glad to help! :)
@TytireRecubans I've posted an answer on that question, see if that works for you.
|
0

Here's an example of the solution posted by 404 that checks what we are trying to insert in our db is a NITS (nothing interesting to say), and replace it with NULL.

CREATE TABLE COMPANY(
   ID             VARCHAR,
   NAME           VARCHAR,
   AGE            VARCHAR,
   ADDRESS        VARCHAR,
   DATE           INTEGER
);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, DATE)
VALUES (CASE WHEN cast('a' as text) IN ('', '#', '-', '--', '??') THEN NULL ELSE 'a' END,
        CASE WHEN cast('gino' as text) IN ('', '#', '-', '--', '??') THEN NULL ELSE 'gino' END,
        CASE WHEN cast('' as text) IN ('', '#', '-', '--', '??', 'na') THEN NULL ELSE '' END,
        CASE WHEN cast('via figa' as text) IN ('', '#', '-', '--', '??') THEN NULL ELSE 'via figa' END,
        CASE WHEN cast(1 as text) IN ('', '#', '-', '--', '??') THEN NULL ELSE 1  END);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.