Your columns may have a categorization scheme in the form of an alphanumeric number. and it may be needed to have a customized incremental value for this scheme. Consider that you want to have numbers in the series like ABC1, ABC2, ABC3 etc. In SQL Server, there can be many methods to do this.
In this post, I will show two methods to increment an Alphanumeric Number in SQL Server
declare @t table(id int identity(1,1), names varchar(100))
insert into @t(names)
select 'test1' union all
select 'test2' union all
select 'test3' union all
select 'test4' union all
select 'test5'
select 'ABC'+CAST(id as varchar(10)) as id,names from @t
The above method concatenates the string with the identity column. This produces the following results:
Method 2 : Use Derived column in the table
declare @t table(id int identity(1,1),
alpha_id as 'ABC'+CAST(id as varchar(10)), names varchar(100))
insert into @t(names)
select 'test1' union all
select 'test2' union all
select 'test3' union all
select 'test4' union all
select 'test5'
select * from @t
Using this method, the T-SQL code concatenates the string ABC with identity column as soon as data is added to the table.
No comments:
Post a Comment