Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2007, 02:58 AM
Registered User
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Identity creation in SQL

hi everybody,
    I have to know that how to generate the Identity with Alphabets like (bc00001,gsa00001).How to write the code in SQL Server 2000.Thanks In Advance.
Regards
Srinivasan.R
 
Old December 28th, 2007, 06:34 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

There is no way to auto generate alphanumric identity, you have to do it manually.
First you must fix length of alpha chars and length of numeric chars.

eg.
student table

STUDCODE STUDNAME
STD00001 Ramesh Mehta
STD00002 Samir Patel


Here alpha length is 3 and number length is 5
Declare @newnumber INTEGER
DECLARE @newstdcode varchar(8)

--find max number
SELECT @newnumber=ISNULL(MAX(substring(STUDCODE,4,8)),0)+ 1 FROM STUDENT

--prepare your code by padding 0's
@newstdcode ='STD'+replicate('0',5-len(str(@newnumber,5,0)))

INSERT INTO STUDENT (STUDCODE,STUDNAME) VALUES (@newstdcode,'New Student name')

urt
 
Old December 28th, 2007, 02:51 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

And, just to note... having a code that looks like STD00001 is the absolute worst practice you can have because there is the eventuality of needing to recycle numbers because the current format of the code only allow 99,999 unique numbers. Really bad practice...

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sql Server Function Creation Arunachalam SQL Server 2000 5 May 31st, 2006 10:10 AM
Inserting a Value into an SQL Identity Field in VB vbmazza VB Databases Basics 1 April 27th, 2005 02:45 PM
SQL Server Identity field won't increment Ron Howerton VB.NET 2002/2003 Basics 8 April 22nd, 2004 12:14 PM
Identity soccers_guy10 SQL Server 2000 3 September 2nd, 2003 07:05 AM
User Creation in SQL Server Database angie C# 1 June 12th, 2003 04:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.