Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 GetNewID

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-01-05 : 09:35:15

Can anybody modify the follwing procedure?
I would like to get new id like
ABC00001

Create PROCEDURE [dbo].[GetNewId]
AS
SELECT ISNULL(MAX(
CONVERT(INT,replace(ID,'ABC',''))),0) +1 AS Id
FROM tblA

balaganapathy.n
Starting Member

18 Posts

Posted - 2010-01-05 : 10:11:40
Try this

SELECT 'ABC' + SUBSTRING( CONVERT( VARCHAR, MAX(CONVERT(INT, (REPLACE(ISNULL(ID, 'ABC00001') , 'ABC', '')) ) + 100000) + 1 ), 2, 5 )
FROM tblB

This wont return any value unless you manually insert a row with value 'ABC00001' in the table tblA


balaganapathy n.

Anything you can imagine is real.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-01-05 : 11:15:31
Thanks Balaganapathy.n
what are then last two parameters (2,5)
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2010-01-05 : 11:17:58
Those are part of the substring function, start at position 2 and go 5 positions further.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-06 : 01:14:16
Also Refer this

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 03:30:07
also see
http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
Go to Top of Page
   

- Advertisement -