Solved

Changing the data type of a column

Posted on 2006-11-28
6
1,043 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

615 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