extracting the middle values of a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hillu
Forum Newbie
Posts: 13
Joined: Sun Jun 15, 2008 11:06 am
Location: Addis Ababa

extracting the middle values of a string

Post by Hillu »

Hi, I have a table which contains employees. Each employee has a code. The code indicates the department the employee belongs to the year he joined the organization and a unique number. The format is three digits alphabets, two digits indicating the year and two digits the unique number. Like ‘ABC0503’.

I wanted the digits representing the year and I used this query.

$query = "SELECT SUBSTRING(code,4,5) AS acc
FROM tblEmployee
WHERE (SUBSTRING(code,1,3)= 'ABC' OR SUBSTRING(code,1,3)='XYZ' ";

$result = @mysql_query($query);
$nbrows_batch = @mysql_num_rows($result);
if ($nbrows > 0){
while($rowdata= @mysql_fetch_array($result)){
$empCode = $rowdata['acc'];
}//while($rowdata = @mysql_fetch_array($result))

But I am getting the whole 4 numeric characters. Any suggestiona?

Thanks.
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: extracting the middle values of a string

Post by Dynamis »

Any reason why you can't just pull the whole code from the DB and then use:

Code: Select all

substr(code,2,2)
on the code to get the year?

Where the first 2 represents where to start and the second 2 indicates that you want to get the next 2 characters (the year).
Hillu
Forum Newbie
Posts: 13
Joined: Sun Jun 15, 2008 11:06 am
Location: Addis Ababa

Re: extracting the middle values of a string

Post by Hillu »

thanks a lot. It works.
Post Reply