perform query based on part of text

P

papa jonah

I have a table that has a field (org) that has data like
AA-2
AA-2
AA-2
AA-1
AA-D
NMT-D
NMT-4
NMT-4

I want a query that will list all of the organizations (1 time) that
are represented to the left of the dash.
For example, the results of the query on the above table would be
AA
NMT

TIA
 
F

fredg

I have a table that has a field (org) that has data like
AA-2
AA-2
AA-2
AA-1
AA-D
NMT-D
NMT-4
NMT-4

I want a query that will list all of the organizations (1 time) that
are represented to the left of the dash.
For example, the results of the query on the above table would be
AA
NMT

TIA

SELECT DISTINCT IIf(InStr([Org],"-")>0,Left([Org],InStr([Org],"-")-1))
AS Exp
FROM YourTableName;
 
P

papa jonah

That almost got me where I need to go. I did not characterize my table
well enough before as I forgot to mention that not all orgs have a "-".
For example, EO is just EO. The query only reports back those that had
"-" to begin with.
How can I adjust this to also include those orgs that do not have
dashes in them?

If necessary, I can change the field that I select from, in which case
I may be able to get away with just identifying the orgs (from the
other field) - again I need to limit the response to just once per org.

In the above code from fredg, I understand the if statement, but I do
not understand what about the code limits the results from duplicating.

TIA
 
J

John Spencer

Simple - change the IIF statement so it returns the Org if there is no dash.

IIf(InStr([Org],"-")>0,Left([Org],InStr([Org],"-")-1),[Org])
 
P

papa jonah

That works great. Thank you. Can you tell me what it is about this
code that limits results to once per.
For example, in my original post, I listed AA-2 three times, but the
result of the query only shows AA once. That is great, but I'd like to
know why the query works.

Thanks again
 
F

fredg

That works great. Thank you. Can you tell me what it is about this
code that limits results to once per.
For example, in my original post, I listed AA-2 three times, but the
result of the query only shows AA once. That is great, but I'd like to
know why the query works.

Thanks again

Select DISTINCT etc.......
 

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

Top