Solved

Changing the data type of a column

Posted on 2006-11-28
6
1,042 Views
Last Modified: 2013-12-03
Experts,

I am updating a column datatype integer to varchar(3) with the following script

ALTER TABLE mythirdTable
            ALTER COLUMN [Column2] [varchar] (3) NOT NULL


after changiging the column type,  i am inserting values in to my table and still getting the error related to the data type of the column I have changed. Can you please help why this is happening or if I am doing anything wrong?

INSERT INTO mythirdTable
(Column1,Column2,Column3,Column4,Column5)
VALUES
('3Jh45','2G8' ,'Test',10,4)


Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value '2G8' to a column of data type int.

Thanks
0
Comment
Question by:Tpaul_10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:SweatCoder
ID: 18031715
Try this instead:

ALTER TABLE mythirdTable
          ALTER COLUMN [Column2] varchar(3) NOT NULL
0
 
LVL 7

Assisted Solution

by:LandyJ
LandyJ earned 100 total points
ID: 18031736
The command should be;

ALTER TABLE mythirdTable
          ALTER COLUMN [Column2] varchar(3) NOT NULL
0
 
LVL 11

Expert Comment

by:SweatCoder
ID: 18031738
better yet:

ALTER TABLE mythirdTable
          ALTER COLUMN [Column2] char(3) NOT NULL

for small, normally-fixed number of chars, use "char" instead of "varchar" for optimization purposes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tpaul_10
ID: 18032516
Thanks for helping me out guys and I am trying to change data type for another column to the same table. But getting the follwing error

Server: Msg 5074, Level 16, State 1, Line 11
The object 'DF_myThirdTable_Column5' is dependent on column 'Column5'.
Server: Msg 4922, Level 16, State 1, Line 11
ALTER TABLE ALTER COLUMN column5 failed because one or more objects access this column.

I ran to sp_help to see what's this and here are the details

Constraint_type : DEFAULT on column Column5
Constraint_name : DF_myThirdTable_Column5
delete_action : n/a
update_action : n/a
status_enables : n/a
status_for_replication : n/a
Constraint_keys : (0)


so can i use the following script (wich is incomplete)

==================================================================
ALTER TABLE myThirdtable
DROP CONSTRAINT DF_myThirdTable_Column5
==================================================================ALTER TABLE FactorOccupancyTerritoryGroup
            ALTER COLUMN [column5] varchar (3) NOT NULL

================================================================

I am not sure how to add it back once I drop the constraint which has a defaulue as "0"

ALTER TABLE myThirdtable WITH NOCHECK ADD             
      CONSTRAINT [DF_myThirdTable_Column5]
      (
            [Column5]

      ) WITH  FILLFACTOR = 90  ON ..???

can you please tell me or correct my code to add it back the default constraint?

Thanks again for all your wonderful help

0
 
LVL 11

Assisted Solution

by:SweatCoder
SweatCoder earned 200 total points
ID: 18032548
If you ever want to know how to script an existing object, go into Enterprise Manager, right-click on the DB, and choose the "script" option - then you can choose in a granular way exactly which objects you want to script.
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 200 total points
ID: 18035400
ALTER TABLE myThirdtable
DROP CONSTRAINT DF_myThirdTable_Column5

FactorOccupancyTerritoryGroup
          ALTER COLUMN [column5] varchar (3) NOT NULL

ALTER TABLE dbo.myThirdtable ADD CONSTRAINT
      DF_myThirdTable_Column5 DEFAULT 0 FOR Column5
GO
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question