IIf Len Question

J

JIM

I'm trying to conctenate a field in a query for an export file. If length of
CustomerName is 40 I want the leftmost 38 positions and the 40th position in
a concatenated field CustName. Otherwise, if CustomerName is less that 40
positions I want CustName to be the leftmost 39 positions. My SQL is:

IIf (Len([Invoices].[CustomerName])=40, (Left([Invoices].[CustomerName]), 38
& Right ([Invoices].[CustomerName], 1)), Left([Invoices].[CustomerName]), 39)
AS CustName

I keep getting errors and can't get the right syntax. TIA
 
J

John W. Vinson

IIf (Len([Invoices].[CustomerName])=40, (Left([Invoices].[CustomerName]), 38
& Right ([Invoices].[CustomerName], 1)), Left([Invoices].[CustomerName]), 39)
AS CustName

Looks like a parentheis nesting problem. Let's parse this out...

IIf (Len([Invoices].[CustomerName])=40,
(Left([Invoices].[CustomerName]), 38
& Right ([Invoices].[CustomerName], 1)),
Left([Invoices].[CustomerName]), 39)
AS CustName

OK, an extra left paren before the first Left() function call, and no closing
paren. And... lots of other such, for instance you're closing the parentheses
in your Left function calls BEFORE the 38 or 39, the required second argument.

Try

IIf(Len([Invoices].[CustomerName])=40,
Left([Invoices].[CustomerName], 38) & Right([Invoices].[CustomerName], 1),
Left([Invoices].[CustomerName], 39))
AS CustName

all on one line.
 
J

JIM

Thanks John

John W. Vinson said:
IIf (Len([Invoices].[CustomerName])=40, (Left([Invoices].[CustomerName]), 38
& Right ([Invoices].[CustomerName], 1)), Left([Invoices].[CustomerName]), 39)
AS CustName

Looks like a parentheis nesting problem. Let's parse this out...

IIf (Len([Invoices].[CustomerName])=40,
(Left([Invoices].[CustomerName]), 38
& Right ([Invoices].[CustomerName], 1)),
Left([Invoices].[CustomerName]), 39)
AS CustName

OK, an extra left paren before the first Left() function call, and no closing
paren. And... lots of other such, for instance you're closing the parentheses
in your Left function calls BEFORE the 38 or 39, the required second argument.

Try

IIf(Len([Invoices].[CustomerName])=40,
Left([Invoices].[CustomerName], 38) & Right([Invoices].[CustomerName], 1),
Left([Invoices].[CustomerName], 39))
AS CustName

all on one line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Quotation Mark Question 3
Syntax for IIf Len 4
Nested IIf and IsNull 1
Need to refine sort order 2
Len Function 3
Concatenation Formula needed 5
Averages - Golf League Scoring 12
IIf Criteria Question in a Query 4

Top