SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 How to create primary key value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/06/2013 :  05:06:35  Show Profile  Reply with Quote
Hai,

I want to set primary key ID value like '000001' instead 1 and '000002' instead of 2 and so on..

How to create table? kindly help me.

Thanks in advance!

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/06/2013 :  05:14:40  Show Profile  Reply with Quote
create an identity column and then a computed column based on that

something like


CREATE TABLE TableName
(
ID int IDENTITY(1,1),
PKID AS RIGHT('000000' + CAST(ID AS varchar(6)),6) PRIMARY KEY,
...
CONSTRAINT PK_TableName_PKID
               PRIMARY KEY CLUSTERED (PKID)
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 11/06/2013 05:14:58
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/06/2013 :  05:15:43  Show Profile  Reply with Quote
also see

http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/07/2013 :  03:26:47  Show Profile  Reply with Quote
Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/07/2013 :  04:20:21  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/09/2013 :  03:30:38  Show Profile  Reply with Quote
I want to create table like this Member Id value is EM201300001.
But i got error.

create table Celenew1 (ID INT IDENTITY ( 1 , 1 )PRIMARY KEY,
MemberId AS 'EM' + Cast(year(getdate() as varchar) +RIGHT('0000' + CAST( ID as varchar),5),
name varchar(20))

Kindly help me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/09/2013 :  10:40:22  Show Profile  Reply with Quote
there were few syntax errors

create table Celenew1 (ID INT IDENTITY ( 1 , 1 )PRIMARY KEY,
MemberId AS 'EM' + Cast(year(getdate()) as varchar(4)) +RIGHT('0000' + CAST( ID as varchar(5)),5),
name varchar(20))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/13/2013 :  04:58:56  Show Profile  Reply with Quote
Thank you
Go to Top of Page

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/13/2013 :  05:03:41  Show Profile  Reply with Quote
I want to get year last two digit only. that is 2013 means only 13
I used this query. But its display 2013

select datepart(yy,getdate())

Kindly help me
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 11/13/2013 :  05:57:08  Show Profile  Reply with Quote
SELECT RIGHT(YEAR(getdate()), 2) YY

--
Chandu
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 11/13/2013 :  05:58:47  Show Profile  Reply with Quote
select
	[Year] = right(a.Yr,2)
from
	( --Test Data
	select Yr = 1998 union all
	select Yr = 1999 union all
	select Yr = 2000 union all
	select Yr = 2001 union all
	select Yr = 2002 union all
	select Yr = 2004
	) a


--
Chandu
Go to Top of Page

Arunavally
Yak Posting Veteran

India
58 Posts

Posted - 11/13/2013 :  22:03:26  Show Profile  Reply with Quote
Thank you
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000