473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

key column from 0000001 to 9999999

Hi all,

I want to add a column to my db table with numbers starting at 0000001
and going t'ill the end...It has 2.7 millions entries..so it should be
around .. 2700000. I really need the first number to have all the extra
'0's in front...whenever I place a key column with an identity..it
starts a 1 and increments...I need it to start at 0000001. I tried
placing that(0000001) in the identity seed and increment by 1 but it
still start at 1. I'm fairly a begginer in sql server 2000 but can
manage my way around...
I also tried a query:

alter table dbo.tablename
add column columnname int not null
identity(0000001,1)

and that didn't work out either.(just starts at 1)
So if someone could help me out here I would appreciate. Thanks again
for all the help!!
JMT

Jul 23 '05 #1
10 2672
You are making the double mistake of A) assigning some business meaning
to an artificial IDENTITY key and B) performing formatting in the
database rather than in the client or display tier.

Store the value as a number and forget about how it's formatted (you
can always do it in a view if you must) or use a CHAR / VARCHAR column
and assign a meaningful key rather than generate one artificially.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
You are making the double mistake of A) assigning some business meaning
to an artificial IDENTITY key and B) performing formatting in the
database rather than in the client or display tier.

Store the value as a number and forget about how it's formatted (you
can always do it in a view if you must) or use a CHAR / VARCHAR column
and assign a meaningful key rather than generate one artificially.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3
This is just for a prototype that I'm building. I don't really care who
gets what number and in what order but I need a column that has 7
digits for every row and every row must have a different number. Is
this possible ?? My column is already a VARCHAR.
I am just wondering if it's possible to do something like this ??
I understand that you're trying to teach me something that would be
better for databases in general but I really just need this to work...
THanks alot for the reply!
JMT

Jul 23 '05 #4
This is just for a prototype that I'm building. I don't really care who
gets what number and in what order but I need a column that has 7
digits for every row and every row must have a different number. Is
this possible ?? My column is already a VARCHAR.
I am just wondering if it's possible to do something like this ??
I understand that you're trying to teach me something that would be
better for databases in general but I really just need this to work...
THanks alot for the reply!
JMT

Jul 23 '05 #5
If this is just a one-off:

DECLARE @x VARCHAR(7)
SET @x = 0

UPDATE YourTable
SET @x = col = RIGHT('0000000'+CAST(@x + 1 AS VARCHAR),7)

This is undefined behaviour so don't rely on it in any persistent code.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #6
If this is just a one-off:

DECLARE @x VARCHAR(7)
SET @x = 0

UPDATE YourTable
SET @x = col = RIGHT('0000000'+CAST(@x + 1 AS VARCHAR),7)

This is undefined behaviour so don't rely on it in any persistent code.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #7
I would like to know how to do this in SQL Server also. I scanned the
online documentation but I didn't find a useful example. In Oracle
this is real easy:

UT1 > select to_char(123,'000009') as CNUM from dual;

CNUM
-------
000123

I have got to believe that there is a fairly simple way to do this in
SQL Server via a couple of provided functions but I haven't been able
to figure it out yet looking at CONVERT and STR. Who can save me a
couple of hours?

-- Mark D Powell --

Jul 23 '05 #8
Thanks alot David, this does the job just fine. It was just what I was
looking for...

THanks again,
JMT

Jul 23 '05 #9
vbnetrookie (bi****@hotmail.com) writes:
This is just for a prototype that I'm building. I don't really care who
gets what number and in what order but I need a column that has 7
digits for every row and every row must have a different number. Is
this possible ?? My column is already a VARCHAR.
I am just wondering if it's possible to do something like this ??
I understand that you're trying to teach me something that would be
better for databases in general but I really just need this to work...
THanks alot for the reply!


This is quite easy actually. Drop your varchar column as it is now. Then
say:

ALTER TABLE tbl ADD
ident int IDENTITY,
displaykey AS RIGHT('0000000'+CAST(ident + 1 AS VARCHAR),7)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #10
Thank you, Erland

Using the Northwind Db for testing

select RIGHT('0000000'+CAST(CategoryID AS VARCHAR),7), CategoryId
from Categories

0000001 1
0000002 2
0000003 3
0000004 4
0000005 5
0000006 6
0000007 7
0000008 8

-- Mark D Powell --

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nicolas Favre-Félix | last post by:
Hello, I have a little problem using Tkinter: I'd like to make a interface with 3 labels on the left, facing with 3 Entry on the right, a button below Entrys, and a Listbox under all. I could...
4
by: Thomas Jerkins | last post by:
When I write a create table SQL statement I want to add some information about the column heading of each column. How does the exact syntax for the create table look like (which includes this column...
9
by: Eric | last post by:
I would like to setup a two column layout for my web page using CSS. I would like the Left column width to be auto. Can I have the right column be offset by 8 pixels from the right edge of the...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
7
by: Douglas Buchanan | last post by:
I cannot access certain column values of a list box using code. I have a list box 'lstPrv' populated by the query below. SELECT tblPrv.fkPrvID, lkpCat.CatNm, lkpSrv.SrvNm, lkpCat.pkCatID,...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
2
by: ricky | last post by:
Hello, If anyone could help me with this I would highly appreciate it. I've tried everything and nothing works. What I am trying to do is so damn basic and it's just frustrating that it seems...
5
by: sameer_deshpande | last post by:
Hi, I need to create a partition table but the column on which I need to create a partition may not have any logical ranges. So while creating or defining partition function I can not use any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.