久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將存儲為 VARCHAR 的 BINARY 轉(zhuǎn)換為 BINARY

Convert a BINARY stored as VARCHAR to BINARY(將存儲為 VARCHAR 的 BINARY 轉(zhuǎn)換為 BINARY)
本文介紹了將存儲為 VARCHAR 的 BINARY 轉(zhuǎn)換為 BINARY的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我從表(源)中執(zhí)行 INSERT SELECT,其中每一列都是 VARCHAR 數(shù)據(jù)類型.

I do an INSERT SELECT from a table (source) where every column is of VARCHAR datatype.

其中一列存儲二進制數(shù)據(jù),如

One of the columns stores binary data like

'0003f80075177fe6'

我插入的目標表具有相同的列,但具有正確的 BINARY(16) 數(shù)據(jù)類型.

The destination table, where I insert this, has the same column, but with proper data type of BINARY(16).

INSERT INTO destination
(
    column1,        --type of BINARY(16)
    ...
)
SELECT
    CONVERT(BINARY(16),[varchar_column_storing_binary_data]),   --'0003f80075177fe6'
FROM source
GO

當我插入它,然后選擇目標表時,我從 BINARY16 列中得到了一個不同的值:

When I insert it, then select the destination table, I got a different value from the BINARY16 column:

0x30303033663830303735313737666536

這看起來不像是同一個值.

It does not really seems like the same value.

將存儲為 VARCHAR 的二進制數(shù)據(jù)轉(zhuǎn)換為 BINARY 列的正確方法是什么?

What should be the proper way to convert binary data stored as VARCHAR to BINARY column?

推薦答案

你得到的結(jié)果是因為字符串0003f80075177fe6"(一個 VARCHAR 值)被轉(zhuǎn)換為代碼點,而這些代碼點作為二進制值提供.由于您可能正在使用與 ASCII 兼容的排序規(guī)則,這意味著您將獲得 ASCII 代碼點:0 是 48(30 進制),f 是 102(66 進制)等等.這解釋了 30 30 30 33 66 38 30 30...

The result you get is because the string "0003f80075177fe6" (a VARCHAR value) is converted to code points, and these code points are served up as a binary value. Since you're probably using an ASCII-compatible collation, that means you get the ASCII code points: 0 is 48 (30 hex), f is 102 (66 hex) and so on. This explains the 30 30 30 33 66 38 30 30...

您想要做的是將字符串解析為字節(jié)的十六進制表示 (00 03 f8 00 75 71 77 fe 66).CONVERT 接受額外的樣式"允許您轉(zhuǎn)換十六進制字符串的參數(shù):

What you want to do instead is parse the string as a hexadecimal representation of the bytes (00 03 f8 00 75 71 77 fe 66). CONVERT accepts an extra "style" parameter that allows you to convert hexstrings:

SELECT CONVERT(BINARY(16), '0003f80075177fe6', 2)

樣式 2 將十六進制字符串轉(zhuǎn)換為二進制字符串.(樣式 1 對以0x"開頭的字符串執(zhí)行相同的操作,但此處并非如此.)

Style 2 converts a hexstring to binary. (Style 1 does the same for strings that start with "0x", which is not the case here.)

請注意,如果少于 16 個字節(jié)(如本例中所示),則該值右填充零(0x0003F80075177FE60000000000000000).如果您需要左填充,則必須自己執(zhí)行此操作:

Note that if there are less than 16 bytes (as in this case), the value is right-padded with zeroes (0x0003F80075177FE60000000000000000). If you need it left-padded instead, you have to do that yourself:

SELECT CONVERT(BINARY(16), RIGHT(REPLICATE('00', 16) + '0003f80075177fe6', 32), 2)

最后,請注意,二進制文字可以在不進行轉(zhuǎn)換的情況下簡單地通過在它們前面加上0x"而不使用引號來指定:SELECT 0x0003f80075177fe6 將返回類型為 BINARY(8).與此查詢無關,只是為了完整性.

Finally, note that binary literals can be specified without conversion simply by prefixing them with "0x" and not using quotes: SELECT 0x0003f80075177fe6 will return a column of type BINARY(8). Not relevant for this query, but just for completeness.

這篇關于將存儲為 VARCHAR 的 BINARY 轉(zhuǎn)換為 BINARY的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

SQL trigger on Truncate(截斷時的 SQL 觸發(fā)器)
sql search query with multiple optional search parameters(具有多個可選搜索參數(shù)的 sql 搜索查詢)
SQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP(SQL 效率:WHERE IN 子查詢 vs. JOIN 然后 GROUP)
Retrieving XML element name using t-SQL(使用 t-SQL 檢索 XML 元素名稱)
Insert double quotes into SQL output(在 SQL 輸出中插入雙引號)
Delete rows from CTE in SQL SERVER(從 SQL SERVER 中的 CTE 中刪除行)
主站蜘蛛池模板: 在线观看 亚洲 | 综合在线视频 | 在线视频一区二区 | 五月综合激情婷婷 | 精品九九| 精品欧美激情在线观看 | 欧美aaaa视频 | 国产精品一区二区三区久久 | 中文字幕在线观看 | 日韩成人在线免费视频 | 久久久久国产 | 69性欧美高清影院 | 国产一区二区欧美 | 亚洲欧洲一区 | 日本a级大片 | 中文字幕日韩av | 亚洲成人免费视频 | 精品一区二区在线观看 | 亚洲欧美在线免费观看 | 亚洲国产区| 午夜欧美日韩 | 在线视频一区二区三区 | 精品视频一区二区三区在线观看 | 国产一区二区在线视频 | 成人综合一区二区 | 日韩亚洲视频 | 亚洲成人综合社区 | 国产一区二区在线播放 | 亚洲精选久久 | 精品亚洲二区 | 奇米久久久 | 日本一区二区三区免费观看 | 亚欧精品 | 精品美女| 二区在线观看 | 在线视频亚洲 | 国产精品一区久久久 | 欧美a级成人淫片免费看 | 中文字幕成人免费视频 | 成人久久久久 | 欧美一区精品 |