Fetch Database Value

Is there any way that I can fetch the last item code in the series before inserting a new code.
I want to generate a new code in the selected group, before generating a new code, I want to know the last code in that series so that I can add one more to create a new.