Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have data base field intr_prod_id char(10) Now i want to generate the Primary key like IP-1. After 1P-10 it will not increamenting.
I write the Query for this above id Generation IP-10. It will increaming upto 10 after that it will not increamenting
SQL
Select isnull(max(substring(intr_prod_id,4,50)),0)+1 From InterimProducts
Posted
Updated 26-Dec-11 20:06pm
v4
Comments
milannaughty 27-Dec-11 1:11am    
Can you please describe your question.

1 solution

Try This:

SQL
DECLARE @tbl TABLE ( StaffNumber varchar(10) )
INSERT INTO @tbl VALUES ( 'IP-1' )
INSERT INTO @tbl VALUES ( 'IP-9' )

SELECT TOP 1
CASE WHEN ISNUMERIC(SUBSTRING(StaffNumber,PATINDEX('%[0-9]%',StaffNumber),LEN(StaffNumber)))= 1 THEN
REPLACE
(
StaffNumber,
SUBSTRING(StaffNumber,PATINDEX('%[0-9]%',StaffNumber),LEN(StaffNumber)),
SUBSTRING(StaffNumber,PATINDEX('%[0-9]%',StaffNumber),LEN(StaffNumber))+1
)
ELSE
StaffNumber + '1' END AS NewstaffNumber,
SUBSTRING(StaffNumber,PATINDEX('%[0-9]%',StaffNumber),LEN(StaffNumber))+1
FROM @tbl ORDER by 2 desc


More Info:

http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=IncrementAlphaNumericValues[^]
 
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