I want to change the datatype of a field from Varchar to integer in T-SQL.

I tried something like Modify like you use in Oracle.

ALTER TABLE tablename

But its not working in SQL server..

Any thoughts.

Recommended Answers

All 12 Replies

i am not very sure is it different as in SQL where yu can go to the database that you want and then you go to the table and then you click the right side of yur mouse and go to design table and change it

I changed it. I went to enterprise Manager and went to design view of the table and changed the data type of the field.

Thanks anyway

Thanks Wchitamb.

G'd evening!
In case you still need to change the table's column type from T-SQL , the sintax for MS SQL is:
ALTER TABLE table ALTER COLUMN column_name new_data_type

For more about the Alter Table sintax, read the books on line (BOL), here is the last update for SQL 2k SQL Server Books Online January 2004 Update
Good luck

thank u ESTUARDO for sending t-satatment on changing data type

ALTER TABLE table ALTER COLUMN column_name new_data_type
this will definitely work i got answer
thank you Estuardo

1 Question... how does 1 do this for a column with the data type char if you want tot take it to decimal... gives me an error on the conversion....

"Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."

is there an additional way to write in a CONVERT for the data included in the field?

I know this is old, but you know good old Google...

You must first parse the data in the column to ensure that all values will 'implicitly' convert to the new data type.
simple sql to review all rows that will not convert:

SELECT <column> WHERE IsNumeric(<column>) = 0

Once all values in the column can be implicitly converted to the new data type, the alter column statement will complete.

For other readers, note that certain data types and values can only be implicitly converted to specific types and this method may require first a conversion to varchar and then to your desired type.
For some types, you will not be able to use implicit conversion at all, in these cases you will may need to do it the MS way... write the data out to a temp table, drop rows, alter table then write back the rows...

ALTER TABLE table ALTER COLUMN column_name new_data_type

ALTER TABLE table ALTER COLUMN column_name new_data_type

sir i have typed the query you refered but there is error near datatypr

Some data types need you to define the seed number or how many decimal places you want to store. Taking this into account do some research on the decimal datatype as you have to define how many decimal places you want to store if you want to use decimal datatype.

alter table tablename
alter column columnname datatype
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.