Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Id is:

S001
S1102
S546
S999
S745

i am use this query

"Select max(receipt_no) as rcptno from tb_receipt"

every time return
>>> S999

but correct id is S1102


How to get correct id???
Posted
Comments
RahulMGunjal 1-May-14 4:53am    
Its giving you string sorted and not numbers

SQL does exactly what one should expect. It returns the high value for that type. You think of numeric sorting, but while using string you got string sorting.
You may remove the leading 'S' and let SQL do a numeric sorting...
SQL
SELECT MAX(CAST(REPLACE(RECEIPT_NO, 'S', '') AS INT)) ...
 
Share this answer
 
Comments
DamithSL 1-May-14 3:18am    
you assume all the receipt_no start with letter S.
Kornfeld Eliyahu Peter 1-May-14 3:20am    
I see what OP shows...The basic idea is to turn it into int, not matter how, than run max on the int...
You have to keep the string and the number element in the sorting separate.
Sort on the number since you want the larger numbered item.
 
Share this answer
 
Comments
Manamohan Jha 1-May-14 2:45am    
How to short correct
Access and sql db
try below
SQL
select top 1 RECEIPT_NO from
tb_receipt order by
CInt(Mid(RECEIPT_NO, 2, len(RECEIPT_NO))) desc


check below answers
http://stackoverflow.com/questions/15969465/picking-the-maximum-value-from-an-alpha-numeric-column-in-ms-access[^]
 
Share this answer
 
v6
Comments
Manamohan Jha 1-May-14 2:48am    
i think replace is better idea
DamithSL 1-May-14 3:23am    
replace is better if you know exactly what you need to replace, if all the elements start with the same letter; we can omit that letter and insert as number. i don't think OP having such database table. I assume there may be some other characters like S given here.
Manamohan Jha 1-May-14 4:09am    
substring is not working in access database
DamithSL 1-May-14 4:16am    
now only you tell this is access database, next time when you ask question please tag it correctly. You will not get fast and correct answer if not tagged correctly.
Manamohan Jha 1-May-14 4:28am    
Okey, Sorry Sir

I want solution for both db(Sql or Access)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900