Solved

ALTER Table command to change datatype and default value

Posted on 2010-09-20
2
577 Views
Last Modified: 2012-08-13
Hi all -
I have a table X with a column Y of datatype varchar and a default value of '1'.
The column contains existing numerical data but of type varchar.
How do I alter the column to change datatype to INT and update the default value to 1 (instead of '1')  ?
How do I change the existing data in the column to INT ?
0
Comment
Question by:deirekn
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33715271
declare @sql nvarchar(max)
select @sql='alter table X drop constraint ' + object_name(constid) from sysconstraints where id=object_id('X')
exec (@sql);
alter table X alter column Y int;
alter table X add constraint df_X_Y default(1) for Y;
0
 

Author Closing Comment

by:deirekn
ID: 33727246
Thank you. That worked great.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

11 Experts available now in Live!

Get 1:1 Help Now