使用SQL语句检测案例

发布于 2024-11-14 11:22:14 字数 82 浏览 3 评论 0原文

我正在尝试使用 SQL 命令来查看一段文本并确定其中是否包含 3 个连续的大写字母。有办法做到这一点吗?或者更简单的是,SQL 有没有办法检测大小写?

I'm trying to use a SQL command that will look through a block of text and determine if it has 3 consecutive uppercase letters in it. Is there a way of doing this? Or even simpler, is there a way that SQL can detect case?

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

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

发布评论

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

评论(4

左秋 2024-11-21 11:22:14

编辑问题最初被标记为mysql,这会起作用。它已被重新标记为 sql-server,这对于 MS SQL Server 无效。

您可以使用 REGEXP

SELECT columnname REGEXP '[A-Z]{3}' FROM table;

columnname 匹配的情况下返回 1。

EDIT Question was originally tagged mysql and this would've worked. It's been re-tagged as sql-server and this isn't valid for MS SQL Server.

You could use a REGEXP

SELECT columnname REGEXP '[A-Z]{3}' FROM table;

Returns 1 where columnname matches.

千柳 2024-11-21 11:22:14

您可以使用的函数 用法

create function ThreeUpperInARow(@s varchar(max)) returns bit
begin
  declare @Rows int

  ;with cte as
  (
    select left(@s, 3) as Part,
           stuff(@s, 1, 1, '') as Rest
    union all
    select left(Rest, 3) as Part,
           stuff(Rest, 1, 1, '') as Rest
    from cte
    where len(Rest) >= 3
  )
  select @Rows = count(*)
  from cte
  where upper(Part) = Part COLLATE Latin1_General_CS_AS

  return case @Rows when 0
           then 0
           else 1
         end
end

declare @T table(ID int identity, Txt varchar(max))
insert into @T
select 'aaaAFAaaaBB' union all
select 'aaaAAaaaBB'

select T.ID,
       T.Txt,
       dbo.ThreeUpperInARow(T.Txt) as Upp
from @T as T

结果:

ID          Txt             Upp
----------- --------------- -----
1           aaaAFAaaaBB     1
2           aaaAAaaaBB      0

A function you can use

create function ThreeUpperInARow(@s varchar(max)) returns bit
begin
  declare @Rows int

  ;with cte as
  (
    select left(@s, 3) as Part,
           stuff(@s, 1, 1, '') as Rest
    union all
    select left(Rest, 3) as Part,
           stuff(Rest, 1, 1, '') as Rest
    from cte
    where len(Rest) >= 3
  )
  select @Rows = count(*)
  from cte
  where upper(Part) = Part COLLATE Latin1_General_CS_AS

  return case @Rows when 0
           then 0
           else 1
         end
end

Usage:

declare @T table(ID int identity, Txt varchar(max))
insert into @T
select 'aaaAFAaaaBB' union all
select 'aaaAAaaaBB'

select T.ID,
       T.Txt,
       dbo.ThreeUpperInARow(T.Txt) as Upp
from @T as T

Result:

ID          Txt             Upp
----------- --------------- -----
1           aaaAFAaaaBB     1
2           aaaAAaaaBB      0
无风消散 2024-11-21 11:22:14

要扩展迈克尔的答案:您可以使用此构造进行区分大小写的比较:

SELECT 'abc' LIKE BINARY 'ABC';  /*false*/

SELECT 'aBc' LIKE BINARY 'aBc'; /*true*/

请参阅: http://dev.mysql.com/doc/refman/5.5/en/case-sensitivity.html
和: http://dev.mysql.com /doc/refman/5.0/en/string-comparison-functions.html#operator_like

To expand on Michael's answer: you can do a case sensitive comparison using this construct:

SELECT 'abc' LIKE BINARY 'ABC';  /*false*/

SELECT 'aBc' LIKE BINARY 'aBc'; /*true*/

See: http://dev.mysql.com/doc/refman/5.5/en/case-sensitivity.html
and: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

南汐寒笙箫 2024-11-21 11:22:14

中大写字母和小写字母比较相等

我已经通过将 varchars 转换为 varbinary 成功比较了大写字母,因为在 SQL SELECT *
从表
WHERE CAST(SUBSTRING(ColumnName,1,3) as varbinary) = CAST(SUBSTRING(UPPER(ColumnName), 1,3) as varbinary)

这样做的一个问题是,如果您有一个像 O'Reilly 这样的名称,它将返回真的。

I've successfully compared uppercase by casting varchars as varbinary, since uppercase and lowercase compare as equal in SQL

SELECT *
FROM Table
WHERE CAST(SUBSTRING(ColumnName,1,3) as varbinary) = CAST(SUBSTRING(UPPER(ColumnName), 1,3) as varbinary)

One problem with this is that if you have a name like O'Reilly, it will return true.

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