0

I want to insert data into a table only when current dateTime1 column is not equal to the existing dateTime1 column in the table. I tried running the query below but it does not return valid result when mydate1 is null in the table.

INSERT INTO TableA (id, dateTime1, datetime2)
SELECT 123, mydate1, mydate2
WHERE NOT EXISTS (SELECT 1 FROM tableA 
WHERE id=123 and dateTime1 <> mydate1)

Basically I want to insert data into the table only when the date column is not equal to the current date column that we are trying to insert.

1
  • why not using unique constraints?.. and eg insert ... on conflict?.. Commented Nov 30, 2017 at 7:06

1 Answer 1

1

Use COALESCE to avoid NULLs.

So, If you are comparing dates, use constructs like

<>  COALESCE ( mydate1, someotherdate )

where someotherdate is any date which always makes the condition to be false (or true if you need it that way).

Sign up to request clarification or add additional context in comments.

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.