识别拉丁字符

时间:2013-01-28 17:08:44

标签: sql sql-server

以下仅返回一个字符长的姓氏 我可以添加其他条件,以便它只返回拉丁字母字符,即A-Z(加上a-z)

SELECT Surname
FROM WHData.dbo.vw_DimUser
WHERE 
    LEN(Surname) =1
    AND <extra condition required>
GROUP BY Surname

1 个答案:

答案 0 :(得分:0)

SELECT Surname
FROM WHData.dbo.vw_DimUser
WHERE 
    LEN(Surname) = 1
    AND Surname like '[a-Z]'
GROUP BY Surname

Wildcard matching

在以下评论后进行修改:

我们来测试一下:

create table t1 (c1 char(1) collate Latin1_General_CS_AS
                 , c2 char(1) collate Latin1_General_CS_AS
                 , wild1 varchar(10) collate Latin1_General_CS_AS
                , wild2 varchar(10) collate Latin1_General_CS_AS)

insert into t1 values ('A', 'a', '[A-z]', '[a-Z]')

select match1 = case when c1 like wild1 then 'Matched' else 'Unmatched' end
  , match2 = case when c1 like wild2 then 'Matched' else 'Unmatched' end
  , match3 = case when c2 like wild1 then 'Matched' else 'Unmatched' end
  , match4 = case when c2 like wild2 then 'Matched' else 'Unmatched' end
from t1

enter image description here

您可以看到Aa仅在[a-Z]用作匹配模式时才能正确匹配。

SQL Fiddle for example

这可以在the answer to this previous SO question中解释。基本上,对于Latin1_General_CS_AS,SQL服务器将对字符进行排序,如:

a
A
b
B

但是,为了再次窃取这个问题,联机丛书指出:

In range searches, the characters included in the range may vary depending on the sorting rules of the collation.

所以我认为它在某种程度上依赖于COLLATION,所以真的是在任何特定环境中进行测试的情况,其中部署了类似的东西。