# String function for ages

#### tom_crisp

##### Board Regular
Hi there,

I have an age field in a table that has ages formatted like so:
64y
101y
13y 3m
6m 12d

I wish to make a query so that only the year number is left in the field.

So basically take everything before the 'y', unless there is no 'y', in which case put '0'

Heres how I do it currently in excel:

=VALUE(LEFT(H6,FIND("y",H6)-1))

Any help much appreciated.

Tom.

EDIT
I take it that the 'Iif' function can sort the ages that don't have a 'y', its just the other part I need because FIND apparently doesn't exist in Access.

### Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this in a query:

Years: IIf(InStr(1,[Age],"y")=0,0,CInt(Left([Age],InStr(1,[Age],"y")-1)))

Denis

Thanks very much Denis that works great. Shame there isn't more similarity between excel and access functions.

Hi Tom,

Glad it worked. I got frustrated the same way, then I noticed that many of the Access functions are almost identical to the VBA functions in Excel but not the worksheet functions. If you have any experience with VBA, that will help. If not, you'll just have to muddle through and find out as much as you can from posts on this Board and other resources on the Net.

Denis

Denis,
Code:
``Years: IIf(InStr(1,[Age],"y")=0,0,CInt(Left([Age],InStr(1,[Age],"y")-1)))``
Once you took care of the case of no "years" in the field, rather than doing the Instr, etc., the Val() function would have worked just fine. I'm just always looking for less typing

Replies
6
Views
709
Replies
6
Views
317
Replies
7
Views
662
Replies
1
Views
883
Replies
1
Views
1K

1,217,508
Messages
6,137,036
Members
450,041
Latest member
MM2024

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back