help me in substr

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
anaspk
Forum Newbie
Posts: 7
Joined: Wed Mar 25, 2009 1:38 am

help me in substr

Post by anaspk »

can some body help me. i want to subtract a interger value from a varchar value. i mean for example the varchar value is : A-10034/09
i want just 10034 (ignorig "A-" and "/09") for removing "A-" i uses "substr" command.but how can i remove "/09". using len in substr cant fulfill my problem as the lenth of A-*****/09 varies.
<?php
include('datacon.php');
$sq1 = "SELECT regno FROM patientreg";
$result=mysql_query($sq1);
$num=mysql_num_rows($result);
$sn=1;
for ($i=1; $i<=$num; $i++)
{
$row=mysql_fetch_array($result);
$val=$row['regno'];
$sub= substr($val,2);
//echo $sub."<br>";
$sql2="INSERT INTO patientreg(order) VALUES('$sub')";
$query=mysql_query($sql2);
}
?>
and also my subtracting value "$sub" is not inserting in another column of the same table..
or tell me how can i copy a whole column to another empty column of same table with the subtraction of first two and last tree character.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: help me in substr

Post by Bill H »

After you've taken the "A-" off using substr() just use intval() to convert it to a number. That function reads digits and stops when it hits the first non-numeric character, so it will ignore the trailing "/09" without a hiccup.
Post Reply