Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert table column data type from image to varbinary

I have a table like:

create table tbl ( 
  id int,
  data image
)

It's found that the column data have very small size, which can be stored in varbinary(200)

So the new table would be,

create table tbl ( 
    id int,
    data varbinary(200)
)

How can I migrate this table to new design without loosing the data in it.

like image 979
Praveen Avatar asked Jul 04 '26 20:07

Praveen


2 Answers

Just do two separate ALTER TABLEs, since you can only convert image to varbinary(max), but you can, afterwards, change its length:

create table tbl ( 
  id int,
  data image
)
go
insert into tbl(id,data) values
(1,0x0101010101),
(2,0x0204081632)
go
alter table tbl alter column data varbinary(max)
go
alter table tbl alter column data varbinary(200)
go
select * from tbl

Result:

id          data
----------- ---------------
1           0x0101010101
2           0x0204081632
like image 155
Damien_The_Unbeliever Avatar answered Jul 07 '26 11:07

Damien_The_Unbeliever


You can use this ALTER statement to convert existing column IMAGE to VARBINARY(MAX). Refer Here

ALTER Table tbl ALTER COLUMN DATA VARBINARY(MAX)

After this conversion, you are surely, get your data backout.

NOTE:- Don't forgot to take backup before execution.

The IMAGE datatype has been deprecated in future version SQL SERVER, and needs to be converted to VARBINARY(MAX) wherever possible.

like image 35
HaveNoDisplayName Avatar answered Jul 07 '26 10:07

HaveNoDisplayName



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!