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.
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

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 :)
 

Watch MrExcel Video

Forum statistics

Threads
1,111,699
Messages
5,541,271
Members
410,543
Latest member
ExcelGlenn
Top