Converting varchar to tinyint

I accidentally created a new column in an existing table as datatype varchar instead of tinyint.  I cannot seem to find the correct syntax to change the value to the correct datatype.  HELP??
LVL 1
stewmic1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustavo Perez BuenrostroCommented:
Use ALTER TABLE statement as follows:

alter table YourTable alter column YourColumn int not null

PD: Let me know if it works as you expect.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stewmic1Author Commented:
This does not work.  I get the error

 "Incorrect syntax near 'Alter' "
0
Gustavo Perez BuenrostroCommented:
Which SQL flavour are you using?
0
stewmic1Author Commented:
SQL Server 6.5
0
Gustavo Perez BuenrostroCommented:
If you are using SQL 6.5 you can not alter column definition as I mentioned previously. However, you can use INTO clause of SELECT statement to create a new table to correct the column data type definition and insert all rows from the query into it.

Check next code:

-- Something like you have
create table YourTable (col1 int,col2 varchar(3))

insert into YourTable values (1,2)

select * from YourTable
/*
Data in table
col1        col2
----------- ----
1           2
*/

-- Here's what you must do
select col1
      ,'col2'=convert(tinyint,col2)
  into YourTable2
  from YourTable

drop table YourTable

sp_rename YourTable2,YourTable

select * from YourTable
/*
Data in table (with the correct datatype)
col1        col2
----------- ----
1           2
*/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.