求助一个想不通的地方 关于负号-在sql中varchar类型字段的大小比较中的用法

badiku badiku 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
负号 - 有什么特殊意义吗?我看字符中有这个符号时有些诡异

比如


- < 1
3
-a > 1a


a1 < -a2 < a3

这是啥原因?
字符串比较不是逐字符比较的吗?


print @@version

if '-' < '1' print '''-'' < ''1'''
if '-a' < '1a' print '1'
if '-a' = '1a' print '2'
if '-a' > '1a' print '3'
if '-a' > '1a' print '''-a'' > ''1a'''

drop table #test
create table #test (test varchar(20) )
insert #test
select '-1147721273'
union all select '1147721284'
union all select '114772125'
union all select 'a1'
union all select 'a3'
union all select '-a2'
union all select 'a-2'
union all select 'a-4'
union all select 'a4'


select * from #test order by 1




/* 执行结果

Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (X64)
Jul 9 2008 14:17:44
Copyright (c) 1988-2008 Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (VM)


'-' < '1'
3
'-a' > '1a'

(9 行受影响)


(1 行受影响)
test
--------------------
114772125
-1147721273
1147721284
a1
-a2
a-2
a3
a4
a-4

(9 行受影响)



(1 行受影响)
*/
...全文
评论 24
收藏 已收藏
分享
返回顶部
badiku
MS-SQL Server 34,877 成员 254,635 内容