Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,
The values in a table are
P124,P125,9871780998,P126,BPPQ,P127,PBNN ;

A query is:
SQL
select top 1 PatCode
from Patient_Detail
where PatCode  like 'P%'
order by PatID desc


Purpose is to select P127, but it is PBNN. It generate error [reason P+127(numeric)], so please modify the query to exclude PBNN and result is P127.
Posted
Updated 8-Sep-15 1:56am
v2
Comments
Maciej Los 8-Sep-15 7:57am    
Does PatCode is used to store comma separated data?
LebneizTech 8-Sep-15 8:53am    
NO
Michael_Davies 8-Sep-15 8:36am    
If I get the question right then try:

select top 1 PatCode
from Patient_Detail
where PatCode like '%P127%' AND patcode NOT like '%PBNN%'
order by PatID desc

Not certain of the question as can not see your schema can only guess.
LebneizTech 8-Sep-15 8:53am    
The Values in cloumn is not fix, it may P127,PA127,P128,P129,PADV122 ......
but by mistake it may be PA127,PADV122, etc it generate error, so I require to pick P129 in this case.
Maciej Los 8-Sep-15 10:25am    
What kind of error message? I don't get you...

1 solution

How about:

SQL
SELECT top 1 PatCode 
FROM Patient_Detail 
WHERE PatCode LIKE 'P%'
AND ISNUMERIC(SUBSTRING(PatCode, 2,LEN(PatCode)-1))=1
ORDER BY PatID DESC


?
 
Share this answer
 
Comments
LebneizTech 9-Sep-15 7:13am    
Thanks a lot, it is working fine.

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