如何在 sql server 中定位自由文本字段中的 16 位数字字符串?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在 sql server 中定位自由文本字段中的 16 位数字字符串?相关的知识,希望对你有一定的参考价值。
我试图为我每天生成的一份报告制定一个解决方案。 有一个自由文本字段必须被选中,但有可能出现一个16位的CC号。 我需要找到一种方法来定位和排除这些。 有什么好办法吗?
答案
你可以使用 patindex()
:
select patindex('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%', col)
这给了你图案开始的偏移量。
另一答案
DECLARE @CCNumber varchar(20) = '1234 5678 9012 3456' -- CC Number
DECLARE @Table TABLE (FreeTextField varchar(max)) -- Sample Table with one FreeTextField
INSERT INTO @Table VALUES
('This is a text without any CC number')
, ('This a text with the CC number 1234 5678 9012 3456')
, ('This another text without any CC number')
, ('This another text with the CC number 1234 5678 9012 3456')
-- The following script will show only rows without the CC Number
SELECT FreeTextField
FROM @Table
WHERE CHARINDEX(@CCNumber, FreeTextField) = 0
-- If you want to show all rows, but mask the CC Number then do the following
DECLARE @MaskCCNumber varchar(20) = '#### #### #### ####'
SELECT FreeTextField = REPLACE(FreeTextField, @CCNumber, @MaskCCNumber)
FROM @Table
以上是关于如何在 sql server 中定位自由文本字段中的 16 位数字字符串?的主要内容,如果未能解决你的问题,请参考以下文章