3

I have a postgreSQL table which accepts date in yyyy-mm-dd format and it is not accepting if the incoming date format is ''(no date). There could be some instances when '' gets passed as date. Could anyone help me write a function which checks if the incoming date is '' and then replaces it with NULL and then adds it to the db.

5
  • 1
    How would you add it to the database if the column is not null? Commented Jul 16, 2015 at 7:08
  • Apologies for not wording it better. I'd updated the text. what i meant was when a null date is passed. Commented Jul 16, 2015 at 7:10
  • can you show ur insert or update script ? Commented Jul 16, 2015 at 7:11
  • This is a tcl command update control_tracker set date='[regsub -all {\s} [string trim [regsub {\{.*} $whens1_12 ""]] "-" ]' Commented Jul 16, 2015 at 7:15
  • Dates don't have "a format". So "accepts date in yyyy-mm-dd forma*" doesn't really make sense. Which data type exactly is that column? Commented Jul 16, 2015 at 7:20

2 Answers 2

4

Use nullif()

insert into the_table (the_date_column)
values (nullif(?, ''))

Or for an update

update the_table
  set the_date_column = nullif(?, '');
Sign up to request clarification or add additional context in comments.

Comments

0

You could use a case expression to check for this. I'm using :arg to represent the inputting string - change it according to the programming language you're using:

INSERT INTO mytable
(my_date_col)
VALUES (CASE LENGTH(:arg) WHEN 0 THEN NULL ELSE TO_DATE(:arg, 'yyyy-mm-dd' 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.