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

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

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

問(wèn)題描述

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

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

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

One of the columns stores binary data like

'0003f80075177fe6'

我插入的目標(biāo)表具有相同的列,但具有正確的 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

當(dāng)我插入它,然后選擇目標(biāo)表時(shí),我從 BINARY16 列中得到了一個(gè)不同的值:

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

0x30303033663830303735313737666536

這看起來(lái)不像是同一個(gè)值.

It does not really seems like the same value.

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

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

推薦答案

你得到的結(jié)果是因?yàn)樽址?003f80075177fe6"(一個(gè) VARCHAR 值)被轉(zhuǎn)換為代碼點(diǎn),而這些代碼點(diǎn)作為二進(jìn)制值提供.由于您可能正在使用與 ASCII 兼容的排序規(guī)則,這意味著您將獲得 ASCII 代碼點(diǎn):0 是 48(30 進(jìn)制),f 是 102(66 進(jìn)制)等等.這解釋了 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é)的十六進(jìn)制表示 (00 03 f8 00 75 71 77 fe 66).CONVERT 接受額外的樣式"允許您轉(zhuǎn)換十六進(jì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 將十六進(jìn)制字符串轉(zhuǎn)換為二進(jìn)制字符串.(樣式 1 對(duì)以0x"開(kāi)頭的字符串執(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.)

請(qǐng)注意,如果少于 16 個(gè)字節(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)

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

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.

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

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

相關(guān)文檔推薦

SQL trigger on Truncate(截?cái)鄷r(shí)的 SQL 觸發(fā)器)
sql search query with multiple optional search parameters(具有多個(gè)可選搜索參數(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 輸出中插入雙引號(hào))
Delete rows from CTE in SQL SERVER(從 SQL SERVER 中的 CTE 中刪除行)
主站蜘蛛池模板: 夜夜夜久久久 | 亚洲二区在线 | 国产乱码精品一区二三赶尸艳谈 | 国产97在线视频 | 国产av毛片 | 中文字幕第十页 | 亚洲视频免费一区 | 国产精品欧美一区二区三区不卡 | 欧美日韩激情 | 成av人电影在线 | 久久91精品国产一区二区 | 久久一区二区三区四区 | 浮生影院免费观看中文版 | 成人免费看黄网站在线观看 | 99爱国产 | 北条麻妃99精品青青久久主播 | 久久人体视频 | 永久精品| 国产精品久久久久久久久久久久久久 | 欧美一二区 | 91精品久久久久久久久中文字幕 | 国产综合精品一区二区三区 | 日韩精品免费一区二区在线观看 | 亚洲成人在线网 | 自拍视频精品 | 一区二区三区影院 | 日韩一区二区在线视频 | 国产一区二区在线免费观看 | 美女福利视频 | 一级黄色片毛片 | 国产精品一区二区在线 | 99热精品久久 | 色黄爽| 欧美精品在线视频 | 欧美成人a∨高清免费观看 91伊人 | 99精品免费在线观看 | 正在播放一区二区 | 在线视频99 | 国产在线视频一区二区董小宛性色 | 久久久久亚洲精品 | 都市激情亚洲 |