Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well I would like to add prefix to primary key using Computed Column Specifier.
I know there is a formula looks like SL+'_'+cast(id as varchar(20)) but this formula cannot be validated by SQL Server Management Studio.

Please help me to build this formula.

I dont prefer to use coding to generate id for now.

What I have tried:

Tried putting formula SL+'_'+cast(id as varchar(20)) in to Computed Column Specified but not validating due to wrong format..
Posted
Updated 19-Apr-18 7:26am
Comments
Richard Deeming 19-Apr-18 15:49pm    
Is SL a column in your table, or a literal prefix?

If it's a literal prefix, then it needs to be inside the string:
'SL_' + CAST(id As varchar(20))

1 solution

You could use sequences for this, see articles here:
SQL SERVER 2012 – How to generate a varchar Sequence Number – Using Sequence Object | SQL Server Portal[^]
And: Flexible numbering sequences in T-SQL[^]
But sequences are only supported from SQL Server 2012 apparently ...
 
Share this answer
 
v3
Comments
[no name] 19-Apr-18 14:55pm    
+5 for mention the very flexible _sequences_
RickZeeland 19-Apr-18 14:59pm    
Thanks :)

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