日期:2014-05-17  浏览次数:20475 次

T-SQL中大小写区分问题
因为在sql中,sql语句是不区分大小写的,所以如何查找出大写记录或小写记录呢?
例如:Temp表中A列为商品名称,请写出查询以大写字母开头的所有商品名称的T-SQL语句

------解决方案--------------------
通过指定排序规则,就可以 Chinese_PRC_CS_AS,其中的cs表示 大小写敏感

create table temp(prod_name nvarchar(100))

insert into temp
select 'a' union all
select 'A' 


select *
from temp
where prod_name  = 'a' collate Chinese_PRC_CS_AS 
/*
prod_name
a
*/

------解决方案--------------------

create table Temp(id int,A varchar(10))

insert into Temp(id,A)
 select 1,'Book' union all
 select 2,'pen' union all
 select 3,'Phone' union all
 select 4,'Bag' union all
 select 5,'paper'


select id,A from Temp 
 where ascii(left(A,1)) between ascii('A') and ascii('Z')
 
/*
id          A
----------- ----------
1           Book
3           Phone
4           Bag

(3 row(s) affected)
*/