注意一個8字節的二進制(8)列將0x1122334455667788,而不是爲0x12345678。每個字節都有2個十六進制字符。
你可以使用子,如果你已經有一列 「大小」
declare @t table (id int identity, bin binary(8), size int)
insert @t select 0x0102030405060708, 8
insert @t select 0x0102030405000000, 5
insert @t select 0x0102030405060000, 6
insert @t select 0x0102030405060700, 7
insert @t select 0x0102030405060708, 8
select id, CONVERT(varbinary(max), substring(bin, 1, size)) as newbin
from @t
order by id
輸出
id newbin
----------- ---------------------
1 0x0102030405060708
2 0x0102030405
3 0x010203040506
4 0x01020304050607
5 0x0102030405060708
爲您的數據
INSERT INTO newVarBinaryTable (new_id, new_data)
select id, substring(data, 1, size)
FROM oldBinaryTable