Jump to content

SQL Query to turn 0's into blank


rwachowiak

Recommended Posts

whats the easiest way to a query turn 0's (zeros) into "-" in the display?

 

this is my code so far... but say Field 1 is a 0, i want a - there instead

 

but i cant use a and if then statement because the actual query is massive...

 

$query  = "SELECT * FROM table";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo "Instead of 0 this is a -: {$row['field1']}";

}

 

thanks!

Link to comment
Share on other sites

They way you wrote it is a php str_replace

if you wanted to do the replacement in mysql you should use something like this (but don't use that nasty star operator to select all field)

SELECT *, REPLACE(field1,'0','-') as Replaced from `table`

 

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

 

http://us2.php.net/str_replace

Link to comment
Share on other sites

They way you wrote it is a php str_replace

if you wanted to do the replacement in mysql you should use something like this (but don't use that nasty star operator to select all field)

SELECT *, REPLACE(field1,'0','-') as Replaced from `table`

 

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

 

http://us2.php.net/str_replace

 

yes perfect, now what about if i got this going on:

 

$id = (isset($_GET['Userid']))? mysql_escape_string($_GET['Userid']) : "1";
$query  = "SELECT (wk1act-week1), week1, wk1act FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id' GROUP BY project.id";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<tr>".
   "<td width='40' height='20' class='vertspreader'><div align='center'>{$row['week1']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['wk1act']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['(wk1act-week1)']}</div></td>" .
}
</tr>

 

thanks!!!

Link to comment
Share on other sites

$id = (isset($_GET['Userid']))? mysql_escape_string($_GET['Userid']) : "1";
$query  = "SELECT (wk1act-week1), REPLACE(week1,'0','-') as week1a, REPLACE(wk1act,'0','-') as wk1acta FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id' GROUP BY project.id";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<tr>".
   "<td width='40' height='20' class='vertspreader'><div align='center'>{$row['week1a']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['wk1acta']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['(wk1act-week1)']}</div></td>" .
}
</tr>

Link to comment
Share on other sites

$id = (isset($_GET['Userid']))? mysql_escape_string($_GET['Userid']) : "1";
$query  = "SELECT (wk1act-week1), REPLACE(week1,'0','-') as week1a, REPLACE(wk1act,'0','-') as wk1acta FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id' GROUP BY project.id";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<tr>".
   "<td width='40' height='20' class='vertspreader'><div align='center'>{$row['week1a']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['wk1acta']}</div></td>" .
   "<td width='40' height='20' class='topspreader'><div align='center'>{$row['(wk1act-week1)']}</div></td>" .
}
</tr>

 

ah nice thats how! perfect!!!!

 

ok... but now it changes 10 to 1-... VERY CLOSE THOUGH!!

 

how can i make it exact?? hmm

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.