Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

i have created a table with 3 rows...
SQL
user_id   int,
user_name varchar(50),
address   varchar(50),
amount    bigint


user_id is primary key.

i have inserted 10 values into table .
Now i want to change amount datatype(bigint) to float..when i try to do this im getting error...can any one get me the solution for this?
Thanks in advance

Regards
Meenakshi
Posted
Updated 11-May-12 12:15pm
v2

You cannot change the datatype of your column directly. You have to do it in several stages :

1- Create another column with the correct datatype
SQL
ALTER TABLE <yourtable> ADD COLUMN newcolumn float


2- Copy the data from the first column to the new one
SQL
UPDATE <yourtable> SET newcolumn = CONVERT(amount, float)

(make sure there are not any value in amount column that exceeds the float datatype boudaries)

3- Delete the amount column
SQL
ALTER TABLE <yourtable> DELETE COLUMN amount


4- Rename the mycolumn to amount (you'll have to search for this - there is a stored procedure, but I can't remember its name)

Hope this helps.
 
Share this answer
 
v2
Comments
Maciej Los 11-May-12 5:18am    
Good answer, my 5!
phil.o 11-May-12 5:34am    
Thanks.
Wendelius 11-May-12 18:25pm    
Exactly.
To clarify a bit, basically you can change the data type of a column, see ALTER TABLE[^] and especially ALTER COLUMN. There are restrictions like:
- the column may not be certain data type
- implicit conversion must exist
- it may not be part of a constraint and so on

Now the bigint isn't implicitly convertible to float (AFAIK) so this could be the cause to your problem. Recreating the column would be the solution as posted by phil.o.
 
Share this answer
 
can u post the error. so that problem can be much more visible.
 
Share this answer
 
Comments
Meenakshi_V 11-May-12 3:53am    
cannot modify table....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900