Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Change data range in Field

Author  Topic 

cccmtx
Starting Member

4 Posts

Posted - 2010-12-21 : 12:33:16
Need some help changing a field that is the primary key of the table.
This field already contains data between '001 'and '999 ', need to change the data so that they begin to be between '0001 'and '9999'. In short, '001 'will be '0001' and so on ...
Thank's in advance.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-12-22 : 05:52:40
select '0'+col from table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-12-22 : 15:53:26
1) Learn the differences between a field and a column. IT is important.
2) turn off the constraints on the tables in the schema that reference this nameless table. Oh, it is very rude not to post DDL.
3) Use the ALTER statement to change the nameless_key from CHAR(3) to CHAR(4) wherever it appears
4) update all those references with a leading '0'
5) restore the constraints. Be sure to have CHECK (nameless_key LIKE '[0-9][0-9][0-9][0-9]') in the referenced table.
6) Do not expect this to be fast.


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -