Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello,
i am working on case monitoring system in which i need to make query which will automatic generate case no. +1 to the previous case entered. Please have a look on my query

SQL
select  'A/' +right(cast(year(GETDATE()) as CHAR(4)), 2)+'/'+ + cast(max(right((CaseNo), 2) )+1 as varchar(50))as caseno from tbl_RecordRequisition



it is working fine but after case no. 100 it is not incrementing to 101.

please help
Posted

check this, you need to increase the size in the Right Function , from 2 to 3
SQL
select  'A/' +right(cast(year(GETDATE()) as CHAR(4)), 2)+'/'+   cast(max(right((@CaseNo),  3 ) )+1 as varchar(50))as caseno  
 
Share this answer
 
Comments
ankitsrist 30-Jan-14 5:37am    
yes i know previously i tried 3 but it is giving this error Conversion failed when converting the varchar value '4/9' to data type int. 4 means year's last digit and 9 means case no.
Karthik_Mahalingam 30-Jan-14 5:42am    
CaseNo is int or varchar ??
ankitsrist 30-Jan-14 5:53am    
varchar
Karthik_Mahalingam 30-Jan-14 6:04am    
cast varchar to int and then try.
ankitsrist 30-Jan-14 6:09am    
yes will you please modify my query
because you are taking right 2 digits of CaseNo so it will take 00 and increment it to 1
try this
SQL
cast(max(right((CaseNo), 3) )+1 as varchar(50))
 
Share this answer
 
Comments
ankitsrist 30-Jan-14 5:36am    
yes i know but if i use 3 it gives this error, i have previously tried 3 but it is giving this error


Conversion failed when converting the varchar value '4/9' to data type int.
SP HINGU 30-Jan-14 5:41am    
it works fine on ms sql.

Declare @CaseNo As int
Set @CaseNo= 100

select 'A/' +right(cast(year(GETDATE()) as CHAR(4)), 2)+'/'+ + cast(max(right((@CaseNo), 3) )+1 as varchar(50))as caseno
ankitsrist 30-Jan-14 6:02am    
yeah it is working but when i enter sample value 100 in table then it gives error.....
change your right function
cast(max(right((CaseNo), 2) )+1 as varchar(50))as caseno

to
cast(max(right((CaseNo), 3) )+1 as varchar(50))as caseno
 
Share this answer
 

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