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

將存儲為 VARCHAR 的 BINARY 轉換為 BINARY

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

問題描述

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

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

其中一列存儲二進制數據,如

One of the columns stores binary data like

'0003f80075177fe6'

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

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 的二進制數據轉換為 BINARY 列的正確方法是什么?

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

推薦答案

你得到的結果是因為字符串0003f80075177fe6"(一個 VARCHAR 值)被轉換為代碼點,而這些代碼點作為二進制值提供.由于您可能正在使用與 ASCII 兼容的排序規則,這意味著您將獲得 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...

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

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 將十六進制字符串轉換為二進制字符串.(樣式 1 對以0x"開頭的字符串執行相同的操作,但此處并非如此.)

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 個字節(如本例中所示),則該值右填充零(0x0003F80075177FE60000000000000000).如果您需要左填充,則必須自己執行此操作:

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)

最后,請注意,二進制文字可以在不進行轉換的情況下簡單地通過在它們前面加上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 轉換為 BINARY的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

SQL trigger on Truncate(截斷時的 SQL 觸發器)
sql search query with multiple optional search parameters(具有多個可選搜索參數的 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 中刪除行)
主站蜘蛛池模板: 一区二区三区久久 | 国产精品一级二级 | 日韩精品不卡 | 欧美丰满少妇 | 欧美福利一区二区 | 欧美精品黄色 | 成人在线免费观看网站 | 成人国产综合 | 国产免费一区二区三区 | 亚洲久久在线 | 成人av网站在线观看 | 成人免费视屏 | 波多野结衣乳巨码无在线观看 | 日本欧美在线观看 | 懂色av懂色av粉嫩av | 亚洲国产一区在线 | 国产不卡视频 | 怡红院久久 | 日日干干 | 亚洲123区 | 免费av在线 | 欧美亚洲激情 | 亚州成人 | 久久久久久久久国产 | 国产成人在线免费观看 | 国产在线欧美 | a在线播放| 精品视频一区二区 | 久久久久久久网 | 成人性生活片 | 91麻豆国产精品 | 日韩av资源 | 日韩精品小视频 | 日本欧美久久久久免费播放网 | 国语av| 9999精品视频 | 日日夜夜精品免费 | 日本中文字幕在线观看 | 中文字幕av一区二区三区谷原希美 | 久久av一区二区三区亚洲 | 中文字幕第7页 |