change value name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a query as an ecample that returns several values in a field....dogs,
cats, and eleph. How do I change the eleph to elephant? thanks
 
If "eleph" is the complete value in the column you can the Switch function to
return "elephant" for "eleph". You can have multiple expressions, so if you
wanted to return "Hippopotamus" for "Hippo" as well, you might have
something like this:

SELECT [AnimalID], [Habitat],
SWITCH([Animal] = "Eleph","Elephant", [Animal] = "Hippo","Hippopotamus",
TRUE,[Animal]) AS [AnimalName]
FROM [Animals];

By using the Boolean constant TRUE for the final expression, if any of the
earlier ones in the list don't evaluate to TRUE then the value in the Animal
column would be returned.

If "eleph" is a substring within the column's value then you can use the
Replace function, but this limits you to replacing one particular substring:

SELECT [AnimalID], [Habitat],
REPLACE([Animal],"eleph","elephant") AS [AnimalName]
FROM [Animals];

If you wanted to replace both "eleph" and "hippo" substrings for instance
then you could write a simple VBA function which calls the Replace function
twice and returns a new string with both values replaced:

Public Function ReplaceValues(strOldValue As String) As String

Dim strNewValue As String

strNewValue = Replace(strOldValue, "eleph", "elephant")
strNewValue = Replace(strNewValue, "hippo", "hippotamus")

ReplaceValues = strNewValue

End Function

and call this in the query:

SELECT [AnimalID], [Habitat],
REPLACEVALUES([Animal]) AS [AnimalName]
FROM [Animals];

Ken Sheridan
Stafford, England
 

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

Back
Top