作为对insert control characters in nvarchar or varchar from SQL script?的跟进:
以下SQL脚本中0x前缀十六进制常量与十进制常量之间有什么区别?
看起来像0x前缀的那些不提供整数。我今晚可能会参加另一场比赛,所以请赐教我(:
select
char(0x64) charx64,
char(0100) char100,
nchar(0x64) ncharx64,
nchar(0100) nchar100,
char(0x6564) charx6564,
char(025956) char25956,
nchar(0x6564) ncharx6564,
nchar(025956) nchar25956,
0x64 x64,
0100 d100,
0x6564 x6564,
025956 d25956,
cast(0x64 as varchar) x64varchar,
cast(0100 as varchar) d100varchar,
cast(0x6564 as varchar) x6564varchar,
cast(025956 as varchar) d25956varchar,
cast(0x64 as nvarchar) x64nvarchar,
cast(0100 as nvarchar) d100nvarchar,
cast(0x6564 as nvarchar) x6564nvarchar,
cast(025956 as nvarchar) d25956nvarchar,
null
;
(null
就在那里,因此生成此语句更容易)
结果:
C:\bin>"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S .\SQLEXPRESS -E -i hex-versus-decimal.sql
charx64 char100 ncharx64 nchar100 charx6564 char25956 ncharx6564 nchar25956 x64 d100 x6564 d25956 x64varchar d100varchar x6564varchar d25956varchar x64nvarchar d100nvarchar x6564nvarchar d25956nvarchar
------- ------- -------- -------- --------- --------- ---------- ---------- ---- ----------- ------ ----------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ -----------
d d d d NULL NULL ? ? 0x64 100 0x6564 25956 d 100 ed 25956 d 100 ? 25956 NULL
(1 rows affected)
答案 0 :(得分:2)
对于MS SQL服务器,0x常量是binary literals,而不是整数(binary and varbinary types上的更多)。当你对它们调用char()
和nvarchar()
时,它们会转换为整数(就好像它们是big-endian整数表示,如果我没记错的话)。将它们转换为varchar / nvarchar时,它们被解释为ANSI或UCS2编码文本的字节。