在 Microsoft SQL 服务器中从十六进制文字插入 varbinary 值
insert varbinary value from hex literal in Microsoft SQL Server
我有一个 SpringBoot 应用程序,我在其中使用 jdbcTemplate 向 mssql 插入一行
int numOfRowsAffected = remoteJdbcTemplate.update("insert into dbo.[ELCOR Resource Time Registr_] "
+ "( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] ) "
+ " VALUES (?,CONVERT(varbinary,?),?,?,?,?,?,?,?,?,?,?);",
ELCORResourceTimeRegistr.getEntryNo(),
ELCORResourceTimeRegistr.getEntryNo()),
ELCORResourceTimeRegistr.getPostingDate(),
ELCORResourceTimeRegistr.getResourceNo(),
jobNo,
ELCORResourceTimeRegistr.getWorkType(),
ELCORResourceTimeRegistr.getQuantity(),
ELCORResourceTimeRegistr.getUnitOfMeasure(),
ELCORResourceTimeRegistr.getDescription(),
ELCORResourceTimeRegistr.getCompanyName(),
ELCORResourceTimeRegistr.getCreatedDate(),
0);
ELCORResourceTimeRegistr.getEntryNo()
的值是一个字符串,其值为 0x00173672
但是插入DB的是<30007800 30003000 31003700 33003600 37003200>
ELCORResourceTimeRegistr.getEntryNo().getClass().getCanonicalName() => java.lang.String
documentation for the CONVERT function 表示二进制类型的默认 "style" 是 0
:
Translates ASCII characters to binary bytes, or binary bytes to ASCII characters. Each character or byte is converted 1:1.
所以,
SELECT CONVERT(VARBINARY, '0x00173672') AS foo;
returns
foo
--------------------------------------------------------------
0x30783030313733363732
这是十六进制文字的 ASCII 字节值,而不是十六进制字节本身。为了让 CONVERT 解释 十六进制文字,您需要使用样式 1
,即
SELECT CONVERT(VARBINARY, '0x00173672', 1) AS foo;
哪个returns
foo
--------------------------------------------------------------
0x00173672
我有一个 SpringBoot 应用程序,我在其中使用 jdbcTemplate 向 mssql 插入一行
int numOfRowsAffected = remoteJdbcTemplate.update("insert into dbo.[ELCOR Resource Time Registr_] "
+ "( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] ) "
+ " VALUES (?,CONVERT(varbinary,?),?,?,?,?,?,?,?,?,?,?);",
ELCORResourceTimeRegistr.getEntryNo(),
ELCORResourceTimeRegistr.getEntryNo()),
ELCORResourceTimeRegistr.getPostingDate(),
ELCORResourceTimeRegistr.getResourceNo(),
jobNo,
ELCORResourceTimeRegistr.getWorkType(),
ELCORResourceTimeRegistr.getQuantity(),
ELCORResourceTimeRegistr.getUnitOfMeasure(),
ELCORResourceTimeRegistr.getDescription(),
ELCORResourceTimeRegistr.getCompanyName(),
ELCORResourceTimeRegistr.getCreatedDate(),
0);
ELCORResourceTimeRegistr.getEntryNo()
的值是一个字符串,其值为 0x00173672
但是插入DB的是<30007800 30003000 31003700 33003600 37003200>
ELCORResourceTimeRegistr.getEntryNo().getClass().getCanonicalName() => java.lang.String
documentation for the CONVERT function 表示二进制类型的默认 "style" 是 0
:
Translates ASCII characters to binary bytes, or binary bytes to ASCII characters. Each character or byte is converted 1:1.
所以,
SELECT CONVERT(VARBINARY, '0x00173672') AS foo;
returns
foo
--------------------------------------------------------------
0x30783030313733363732
这是十六进制文字的 ASCII 字节值,而不是十六进制字节本身。为了让 CONVERT 解释 十六进制文字,您需要使用样式 1
,即
SELECT CONVERT(VARBINARY, '0x00173672', 1) AS foo;
哪个returns
foo
--------------------------------------------------------------
0x00173672