Solved

ALTER Table command to change datatype and default value

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Merige returns error code when updating 15 54
Delete from table 6 48
Can Unique column have more than one Null? 8 54
Strange msg in the SSMS pane 13 58
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Backup & Restore 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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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