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"

Recommended Answers

All 7 Replies

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:

<?php

// 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?

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;
or
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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.