如何在 SQL Server 中查找全部大写的值?

发布于 2024-12-15 22:20:45 字数 184 浏览 2 评论 0原文

如何找到全部大写的列值?就像 LastName = 'SMITH' 而不是 'Smith'

这是我正在尝试的...

SELECT *
  FROM MyTable
 WHERE FirstName = UPPER(FirstName)

How can I find column values that are in all caps? Like LastName = 'SMITH' instead of 'Smith'

Here is what I was trying...

SELECT *
  FROM MyTable
 WHERE FirstName = UPPER(FirstName)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

何其悲哀 2024-12-22 22:20:45

您可以强制区分大小写的排序规则;

select * from T
  where fld = upper(fld) collate SQL_Latin1_General_CP1_CS_AS

You can force case sensitive collation;

select * from T
  where fld = upper(fld) collate SQL_Latin1_General_CP1_CS_AS
π浅易 2024-12-22 22:20:45

尝试

 SELECT *
  FROM MyTable
 WHERE FirstName = UPPER(FirstName) COLLATE SQL_Latin1_General_CP1_CS_AS

此排序规则允许区分大小写的比较。

如果要更改数据库的排序规则,以便不需要在查询中指定区分大小写的排序规则,则需要执行以下操作(来自 MSDN):

1) 确保您拥有重新创建用户数据库及其中所有对象所需的所有信息或脚本。

2) 使用 bcp 实用程序等工具导出所有数据。

3) 删除所有用户数据库。

4) 重建 master 数据库,并在安装命令的 SQLCOLLATION 属性中指定新的排序规则。例如:

Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName 
/SQLSYSADMINACCOUNTS=accounts /[ SAPWD= StrongPassword ] 
/SQLCOLLATION=CollationName

5) 创建所有数据库及其中的所有对象。

6) 导入所有数据。

Try

 SELECT *
  FROM MyTable
 WHERE FirstName = UPPER(FirstName) COLLATE SQL_Latin1_General_CP1_CS_AS

This collation allows case sensitive comparisons.

If you want to change the collation of your database so you don't need to specifiy a case-sensitive collation in your queries you need to do the following (from MSDN):

1) Make sure you have all the information or scripts needed to re-create your user databases and all the objects in them.

2) Export all your data using a tool such as the bcp Utility.

3) Drop all the user databases.

4) Rebuild the master database specifying the new collation in the SQLCOLLATION property of the setup command. For example:

Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName 
/SQLSYSADMINACCOUNTS=accounts /[ SAPWD= StrongPassword ] 
/SQLCOLLATION=CollationName

5) Create all the databases and all the objects in them.

6) Import all your data.

提笔书几行 2024-12-22 22:20:45

您需要使用区分大小写的服务器排序规则,如下所示:

SELECT * 
FROM MyTable
WHERE FirstName = UPPER(FirstName) Collate SQL_Latin1_General_CP1_CS_AS

You need to use a server collation which is case sensitive like so:

SELECT * 
FROM MyTable
WHERE FirstName = UPPER(FirstName) Collate SQL_Latin1_General_CP1_CS_AS
无声情话 2024-12-22 22:20:45

默认情况下,SQL 比较不区分大小写。

Be default, SQL comparisons are case-insensitive.

天涯沦落人 2024-12-22 22:20:45

尝试

SELECT *
  FROM MyTable
 WHERE FirstName = LOWER(FirstName)

Try

SELECT *
  FROM MyTable
 WHERE FirstName = LOWER(FirstName)
锦爱 2024-12-22 22:20:45

您可以尝试使用它作为您的 where 子句吗?

WHERE PATINDEX(FirstName + '%',UPPER(FirstName)) = 1

Could you try using this as your where clause?

WHERE PATINDEX(FirstName + '%',UPPER(FirstName)) = 1
等待圉鍢 2024-12-22 22:20:45

看看这里

看来你有几个选项

  • 将字符串转换为 VARBINARY(length)

  • 使用 COLLATE 指定区分大小写的排序规则

  • 计算要比较的字符串的 BINARY_CHECKSUM()

  • 更改表列的 COLLATION 属性

  • 使用计算列(VARBINARY 隐式计算)

Have a look here

Seems you have a few options

  • cast the string to VARBINARY(length)

  • use COLLATE to specify a case-sensitive collation

  • calculate the BINARY_CHECKSUM() of the strings to compare

  • change the table column’s COLLATION property

  • use computed columns (implicit calculation of VARBINARY)

裂开嘴轻声笑有多痛 2024-12-22 22:20:45

试试这个

SELECT *
FROM MyTable
WHERE UPPER(FirstName) COLLATE Latin1_General_CS_AS = FirstName COLLATE Latin1_General_CS_AS

Try This

SELECT *
FROM MyTable
WHERE UPPER(FirstName) COLLATE Latin1_General_CS_AS = FirstName COLLATE Latin1_General_CS_AS
西瑶 2024-12-22 22:20:45

我为此创建了一个简单的 UDF

create function dbo.fnIsStringAllUppercase(@input nvarchar(max)) returns bit

    as

begin

    if (ISNUMERIC(@input) = 0 AND RTRIM(LTRIM(@input)) > '' AND @input = UPPER(@input COLLATE Latin1_General_CS_AS))
        return 1;

    return 0;
end

然后您可以轻松地在 WHERE 子句中的任何列上使用它。

使用OP示例:

SELECT *
FROM   MyTable
WHERE  dbo.fnIsStringAllUppercase(FirstName) = 1

I created a simple UDF for that:

create function dbo.fnIsStringAllUppercase(@input nvarchar(max)) returns bit

    as

begin

    if (ISNUMERIC(@input) = 0 AND RTRIM(LTRIM(@input)) > '' AND @input = UPPER(@input COLLATE Latin1_General_CS_AS))
        return 1;

    return 0;
end

Then you can easily use it on any column in the WHERE clause.

To use the OP example:

SELECT *
FROM   MyTable
WHERE  dbo.fnIsStringAllUppercase(FirstName) = 1
握住你手 2024-12-22 22:20:45

回答这个问题的简单方法是使用排序规则。让我尝试解释一下:

SELECT *
  FROM MyTable
 WHERE FirstName COLLATE SQL_Latin1_General_CP1_CI_AS='SMITH’

在上面的查询中,我使用了 collat​​e,并且没有使用任何内置的 sql 函数,如“UPPER”。原因是因为使用内置函数有其自身的影响。

请找到链接以更好地理解:

Upper 和 Collat​​e 的性能影响

Simple way to answer this question is to use collation. Let me try to explain:

SELECT *
  FROM MyTable
 WHERE FirstName COLLATE SQL_Latin1_General_CP1_CI_AS='SMITH’

In the above query I have used collate and didn’t use any in built sql functions like ‘UPPER’. Reason because using inbuilt functions has it’s own impact.

Please find the link to understand better:

performance impact of upper and collate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文