负号 - 有什么特殊意义吗?我看字符中有这个符号时有些诡异
比如
- < 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 行受影响)
*/
------解决方案--------------------
排序规则影响所有与字符串比较相关的语句,包括各种排序(GROUP BY/PARTITION BY/ORDER BY)、索引内部存储、字符串的比较(=、>、>=、<、<=、<>、LIKE)。特别需要强调的是,LIKE字符串匹配中的范围如'[A-Z]',也依赖于指定的排序规则。
------解决方案--------------------
我也是偷懒,用默认的,但是遇到问题要知道是怎么回事,怎么解决,所以具体解决方法还要看你的需求。
比如我司有一个表中某数据是区分大小写的,且是PK,所以就要把那一列改为CS_AI