Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choosing the Latest project number by the tail

Status
Not open for further replies.

John1Chr

Technical User
Sep 24, 2005
218
US
I have table A in which I want to select the latest project number which is based of the tail.

0395279-00
0395279-01
0395279-02

I want to select the 0395279-02 and ignore the other project. I also have projects that have letters involved like L00P001-03. The tail is usually numeric and looks like -01,-02,-03. It sounds easy but for some reason I’m stumped.
 
a starting point (replace the field and table name):
Code:
SELECT FieldName, RIGHT(FieldName, 2) FROM TableName

will return
[tt]
0395279-00 00
0395279-01 01
0395279-02 02[/tt]

you can use this in conjunction with the MAX function to just get the one you want:

Code:
SELECT FieldName, Max(RIGHT(FieldName, 2)) FROM TableName GROUP BY FieldName
HTH



Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for database developers:
The Fundamentals of Relational Database Design
Understanding SQL Joins
 
leslie, your query will return
Code:
fieldname   max(right(fieldname,2)) 
0395279-00     00
0395279-01     01
0395279-02     02
can you see why? each "tail" is its own max for the entire fieldname

:)

r937.com | rudy.ca
 



I think I'd change mine to ...
Code:
Where Right([Project number],2) = '00'

Skip,

[glasses] [red][/red]
[tongue]
 
SELECT Max([Project Number]) AS LatestProject
FROM [Table A]
GROUP BY Left([Project Number],7)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV! That last one worked perfectly. Everyone had great input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top