Zuhair
Zuhair

Reputation: 287

How to write substr function in active record class of codeigniter model?

I have a id column in a table with a costant prefix,for eg. NC1,NC2,NC3.....I have to calculate the maximum id from the id column.Here is how i do it in mysql -

SELECT max(cast(substr(`column_name`,3) as unsigned)) FROM `table_name` 

Here is how i tried to do in CodeIgniter

$this->db->select_max('cast(substr('column_name',3) as unsigned'),false));
$result=$this->db->get('table_name');

It just doesn't work,What is wrong with the above code?

Upvotes: 1

Views: 2462

Answers (1)

omma2289
omma2289

Reputation: 54619

Try it like this:

$this->db->select_max('cast(substr(`column_name`,3) as unsigned)','max_id');
$this->db->get('table_name');

The second parameter is to rename the resulting field

Upvotes: 1

Related Questions