[英]SQL Server 2008 R2: How to identify and update hexadecimal value
I have the following table called as Hexa_Table
which consists of only one column namely val
. 我有下面的表名为
Hexa_Table
, Hexa_Table
包含一列,即val
。
Table : Hexa_Table
表 :
Hexa_Table
CREATE TABLE Hexa_Table
(
val VARCHAR(50)
);
Insertion : 插入 :
INSERT INTO Hexa_Table VALUES('123456789101213');
INSERT INTO Hexa_Table VALUES('414F2D53594F545641');
INSERT INTO Hexa_Table VALUES('1234F6789A1213G');
INSERT INTO Hexa_Table VALUES('414F2D363530303035');
INSERT INTO Hexa_Table VALUES('12345678910');
Note : Now I want to update only those values which are Hexadecimal
and want to update it to String
, for which I need to identify which are the Hexadecimal
values in the table. 注意 :现在我想只更新那些
Hexadecimal
值,并希望将它更新为String
,我需要确定哪些是表中的Hexadecimal
值。
For example I have record number 2 that is 414F2D53594F545641
if you convert it will get AO-SYOTVA
. 例如,我有记录号2是
414F2D53594F545641
如果你转换它将获得AO-SYOTVA
。 And in 4th record I have 414F2D363530303035
if you convert it will get AO-650005
. 在第4条记录中我有
414F2D363530303035
如果你转换它将得到AO-650005
。
Converted by using : This 通过使用转换: 这
Questions : 问题 :
1. How to identify hexadecimal values in the table? 1.如何识别表中的十六进制值?
2. How to update hexadecimal values to string? 2.如何将十六进制值更新为字符串?
You can use CONVERT
with style 1
. 您可以将
CONVERT
与样式1
一起使用。 Something like this in your scenario. 你的场景中有类似的东西。
You can use Filter NOT LIKE '%[^0-9a-f]%'
to match exact hex pattern and LEN(val) %2 = 0
to check if it has exact number of required bytes for convert
您可以使用Filter
NOT LIKE '%[^0-9a-f]%'
来匹配精确的十六进制模式,并使用LEN(val) %2 = 0
来检查它是否具有convert
所需的确切字节数
SELECT val,
CAST(CONVERT(varbinary(4), '0x' + val, 1) As VARCHAR(100)) as charstring,
CONVERT(varbinary(4), '0x' + val, 1) as HexVal
FROM Hexa_Table
WHERE val NOT LIKE '%[^0-9a-f]%'
AND LEN(val) %2 = 0;
Output: 输出:
val charstring HexVal
414F2D53594F545641 AO-S 0x414F2D53
414F2D363530303035 AO-6 0x414F2D36
Reference 参考
How can I convert a varchar with hexadecimal value to int? 如何将具有十六进制值的varchar转换为int?
You can find hexadecimal values in your table using this: 您可以使用以下命令在表中找到十六进制值:
like '%' + CHAR(0x00) +'%'
However in your example which you have shown, the values are stored already as string so I am not sure what you mean to convert the values as string. 但是在您显示的示例中,值已经存储为字符串,因此我不确定将值转换为字符串是什么意思。
On a side note: 在旁注:
If you want to know how to convert the hex to varchar then you need to use CONVERT like this": 如果你想知道如何将十六进制转换为varchar,那么你需要像这样使用CONVERT“:
CONVERT(VARCHAR(MAX), 0x48656c70)
To find out hexadecimal values you can in following: 要查找十六进制值,您可以在以下内容中找到:
select val
from Hexa_Table
where val like '%' + CHAR(0x00) +'%'
It is already stored as VARCHAR
, what and how do you want to update? 它已经存储为
VARCHAR
,您想要更新的内容和方式?
Try this code: 试试这段代码:
BEGIN TRAN
SELECT * FROM Hexa_Table
WHERE val = @value_insert
IF (@@ROWCOUNT = 0)
INSERT Hexa_Table
SELECT CONVERT(VARCHAR(50), @value_insert)
COMMIT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.