I have a table called "users", in the table it contain a column/ field name "reg_date" how would i run an query to say: For all the users, if their feild "reg_date" is blank put "20th December 2008"

8 Years
Discussion Span
Last Post by =IceBurn=

You could do it in the query itself, but since you're posting in the PHP section, I asume you want to do it using PHP functions.

So, you could do something like this:


// Not tested

$sql = "SELECT TRIM(`reg_date`) FROM `users` ORDER BY `id`";
$qry = mysql_query($sql);

$today = date('jS F Y');

while($obj = mysql_fetch_object($qry)) {

	echo ($obj->reg_date == '') ? $today : $obj->reg_date;



Now if you want to update those blank fields in the table, that's another story! In that case I first need to know what data type are you using in the reg_date field.

Hope this helps.


Can I get an sql query only?

Sure, here you have:

SELECT IF(TRIM(`reg_date`) != '', `reg_date`, DATE_FORMAT(NOW(), '%D %M %Y'))  FROM `records`;

check first whether your reg_date is NULL or not by

mysql> select * from users where reg_date IS NULL;
mysql>select * from users where reg_date = "";

check what it returns in mysql according to that update your table

Hope it helps you


I want to select * from users where "reg_date" IS NULL; and then update "reg_date" with ""20th December 2008"


If the `reg_date` is a varchar or char data type and you allow NULL on that field you can use something like:

UPDATE `reg_date` SET `reg_date` = '20th December 2008' WHERE `reg_date` IS NULL

Correction, assuming your table is named 'users':

UPDATE `users` SET `reg_date` = '20th December 2008' WHERE `reg_date` IS NULL
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.