sql - PATINDEX 表达式
问题描述
我正在创建一个 patindex:
字符 1 必须为 Alpha 且非空格,其余字符应包含任意 AZ、az、空格或'-
patindex('^ [A-Z]%[A-Za-z '-]','Worktester')
但这似乎不起作用
似乎 PATINDEX() 不允许这种模式匹配。
您能否告知这是否正确或任何可以帮助表达的链接?
谢谢
解决方案
有时使用模式匹配的诀窍是找到坏的,而不是好的,模式。要验证长度未知的字符串(而不是提取子字符串),您可以搜索不可接受字符的模式:
declare @Samples as Table ( Sample VarChar(16) );
insert into @Samples ( Sample ) values
( 'Worktester' ), ( 'Foo Bar' ), ( 'Voot-Plex' ),
( 'BR549' ), ( ' Spaced Out' ), ( 'low down' ), ( 'Oops!' );
select Sample,
-- Check the first character for uppercase alpha only.
PatIndex( '[^A-Z]%', Sample collate Latin1_General_BIN ) as Part1,
-- Check the remaining characters for any mix of alpha, space and hyphen.
PatIndex( '_%[^-A-Za-z ]%', Sample collate Latin1_General_BIN ) as Part2,
-- Put it together into a single status.
case
when PatIndex( '[^A-Z]%', Sample collate Latin1_General_BIN ) = 0 and
PatIndex( '_%[^-A-Za-z ]%', Sample collate Latin1_General_BIN ) = 0 then 'Good'
else 'Bad' end as Status
from @Samples;
请注意,默认排序规则往往不区分大小写。Part1
尝试通过删除显式排序规则来更改行。
推荐阅读
- javascript - 如何将带有表单控件的表单组动态添加到表单组
- css - 使用 CSS 显示姓名首字母
- redis-sentinel - 谁重写了slave redis实例的redis配置slaveof?
- java - Firebase 用户 == 一直为空
- cassandra - TestCassandra 中的错误 - 找不到所需的类。...缺少类:com/google/common/util/concurrent/FutureFallback
- ios - 在 Swift 中重写枚举
- c# - 在 cmd 中启动隐藏的 Windows 应用程序而不获取 activeWindows 状态
- python - 如何过滤包含在 PySpark 窗口中的行
- python - 在 pyspark 中调用 UDF 之前无法过滤 Null 值
- angular - 如何为反应式表单控件编写自定义验证器