Solved

ALTER Table command to change datatype and default value

Posted on 2010-09-20
2
582 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
[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
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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