Search This Blog

29 December 2010

How to insert NULL value in PostgreSQL date column?

The simple query is like,
INSERT INTO table (date_field) VALUES (NULL);

But when we try to run this insert query through PHP application, where we don't know whether the variable is NULL or contain some value, it starts giving error. Here we need to do some programming and query manipulation as shown below,

if(empty($dbDate)) $dbDate = '0000-00-00';
$myQry = "INSERT INTO table (date_field) VALUES (CASE $dbDate WHEN 0 THEN NULL ELSE TO_DATE('{$dbDate}', 'YYYY-MM-DD') END)";

/*Remaining code comes here */

No comments:

Post a Comment