Reputation: 237
I have the following:
id | value
1 | abc:432:xyz
2 | 678:123:iop
And like to get:
id | valueA | valueB | valueC
1 | abc | 432 | xyz
2 | 678 | 123 | iop
Is this possible by sql query? Thx!
Upvotes: 2
Views: 461
Reputation: 11
use this
create table test2 as SELECT id,substr(value,1,3) as valueA,substr(value,5,3) as valueB,substr(value,9,3) as valueC FROM test
Upvotes: 0
Reputation: 15603
SELECT id,
SUBSTRING_INDEX(`value`, ':', 1) as valueA,
SUBSTRING(`value`,5,3) as valueB,
SUBSTRING(`value`,9) as valueC
FROM Table_name
use above query.
Upvotes: 0
Reputation: 5402
It's possible using sql query:
SELECT id,SUBSTRING_INDEX(`value`, ':', 1) AS valueA ,
SUBSTRING_INDEX(SUBSTRING_INDEX(value,':',2),':',-1) AS valueB , SUBSTRING_INDEX(SUBSTRING_INDEX(value,':',-2),':',-1) AS valueC
FROM tablename;
Upvotes: 2
Reputation: 7240
one way is to separate them after you read the query results!
Query:
$this->db->query("select id, value from tablename");
Example in PHP (Could be anything):
foreach($resultRecord as $record){
$segment = explode(":",$record->value);
echo "<tr>";
echo "<td>".$record->id;
for($i=0;$i<count($segment);i++){
echo "<td>".$segment[i];
}
}
Upvotes: 0