Decode function

waynered

Board Regular
Joined
Jun 19, 2002
Messages
61
Does anyone know what I need to do in access to replace values in my query results with new values I specify? There is a standard SQL function called "DECODE" but Access does not recognise this.

Any help appreciated
Wayne
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Wayne

Can you explain further what you want to do?

Are you trying to update a field in the underlying table depending on the current value in the field?
 
Upvote 0
Hi,

To emulate the Decode function in Access you need to use multiple IIfs eg

Decode([YourField],"Val1","AltVal1","Val2","AltVal2","OtherwiseThis")

would be

IIf([YourField]="Val1","AltVal1",IIf([YourField]="Val2","AltVal2","OtherwiseThis"))

You could use a custom function, but if your query needs a large number of replacements, VBA would be much slower than IIfs.

HTH, Stubby




 
Upvote 0
Stubby

From what you posted it sounds like DECODE works like the Choose or Switch function.

Is that right?
 
Upvote 0
Hi, Norie

Sort of...Choose & Switch give you "If This Then This", but Decode adds an "Else This", a default value, for unknowns or unspecifiied.

I think Choose requires picking from a list based on the position, so I'm not sure that would work, but Switch would work if a default value isn't needed.

If a default value is needed, & if there are more replacements needed than allowed nested IIfs (seven?), a custom function using select case would probably be the easiest...

Stubby
 
Upvote 0
Stubby

Is there is a limit for nested Iifs?

I know in Excel the nested IF limit is 7.

I've only occasionly used Iifs in Access, normally just 2 or 3.

When you say using a custom function would be slower I think it really depends what is being done.

I think the main problem with a custom function is it is a 'custom' function specific to the particular needs.
 
Upvote 0
Why not use a Decode Table?

If you have a large number of codes to deal with why not put them in a separate Table and just join the primary data table to it to get you decoded values?
 
Upvote 0

Forum statistics

Threads
1,221,851
Messages
6,162,429
Members
451,765
Latest member
craigvan888

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
Back
Top