String function for ages

tom_crisp

Board Regular
Joined
Jul 28, 2006
Messages
133
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
Try this in a query:

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

Denis
 

tom_crisp

Board Regular
Joined
Jul 28, 2006
Messages
133
Thanks very much Denis that works great. Shame there isn't more similarity between excel and access functions.
 

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
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
 

VicRauch

Well-known Member
Joined
Mar 23, 2006
Messages
2,032
Denis,
In your line of code,
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 :)
 

Forum statistics

Threads
1,141,167
Messages
5,704,678
Members
421,361
Latest member
Capper2824

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Top