Solved

Changing the data type of a column

Posted on 2006-11-28
6
1,040 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

832 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