Solved

Changing the data type of a column

Posted on 2006-11-28
6
1,036 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now