# Get select max of a varchar field in cakephp 4

**URL:** <https://discourse.cakephp.org/t/get-select-max-of-a-varchar-field-in-cakephp-4/11754>\
**Category:** Need Help\
**Created:** [December 23, 2023, 11:02pm UTC](https://discourse.cakephp.org/t/get-select-max-of-a-varchar-field-in-cakephp-4/11754 "2023-12-23T23:02:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [December 23, 2023, 11:02pm UTC](https://discourse.cakephp.org/t/get-select-max-of-a-varchar-field-in-cakephp-4/11754/1 "2023-12-23T23:02:39Z")

</div>

I have a table where I save a period as yyyy-mm, this period is a varchar field, when I want to get the max of that field with `$periodMax = $this->Contributions->find('all')->where(['enterprise_id' => $this->enterprise_id])->select(['period' => $queryMax->func()->max('period')])->first();` it only returns the year without the month, how can I do to get the whole value of that field?  
This is what it shows `debug($periodMax)`:

```auto
APP/Controller\ContributionsController.php (line 174)
object(App\Model\Entity\Contribution) id:0 {
  'period' => (float) 2024
  '[new]' => false
  '[accessible]' => [
    'enterprise_id' => true,
    'sword_declaration_number' => true,
    'period' => true,
    'created' => true,
    'modified' => true,
    'enterprise' => true,
    'affiliates_contributions' => true,
  ]
  '[dirty]' => [
  ]
  '[original]' => [
  ]
  '[virtual]' => [
  ]
  '[hasErrors]' => false
  '[errors]' => [
  ]
  '[invalid]' => [
  ]
  '[repository]' => 'Contributions'
}
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 24, 2023, 12:58am UTC](https://discourse.cakephp.org/t/get-select-max-of-a-varchar-field-in-cakephp-4/11754/2 "2023-12-24T00:58:33Z")

</div>

For some reason, it thinks that `period` is a float. You might have to do that thing where you tell the ORM what type something is, if it’s guessing wrong. (I don’t remember how that works; I’ve rarely if ever needed it.)

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [December 24, 2023, 8:44pm UTC](https://discourse.cakephp.org/t/get-select-max-of-a-varchar-field-in-cakephp-4/11754/3 "2023-12-24T20:44:47Z")

</div>

Use the second argument of the `max()` function to specify the return type, by default it’s float.

```php
->max('period', ['string'])

```
