[英]search in a string creditcard numeric value
I want to find a credit card numeric value in a sql string. 我想在sql字符串中找到信用卡数值。
for example; 例如;
DECLARE @value1 NVARCHAR(MAX) = 'The payment is the place 1234567812345678'
DECLARE @value2 NVARCHAR(MAX) = 'The payment is the place 123456aa7812345678'
DECLARE @value3 NVARCHAR(MAX) = 'The payment1234567812345678is the place'
The result should be : 结果应为:
@value1Result 1234567812345678
@value2Result NULL
@value3Result 1234567812345678
16 digits must be together without space. 16位数字必须在一起且不能有空格。
How to do this in a sql script or a function? 如何在sql脚本或函数中执行此操作?
edit : if I want to find these 2 credit card value. 编辑:如果我想找到这2张信用卡值。
@value4 = 'card 1 is : 4034349183539301 and the other one is 3456123485697865'
how should I implement the scripts? 我应该如何实施脚本?
You can use PathIndex as 您可以将PathIndex用作
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]%', yourStr)
if the result is 0 then it doesnt containg 16 digits other was it contains. 如果结果为0,则不包含其他16位数字。
It can be used withing a Where
statement or Select
statement based on your needs 可根据需要将其与Where
语句或Select
语句一起使用
You can write as: 您可以这样写:
SELECT case when Len(LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)) = 16
then LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)
else ''
end
FROM (
SELECT subsrt = SUBSTRING(string, pos, LEN(string))
FROM (
SELECT string, pos = PATINDEX('%[0-9]%', string)
FROM table1
) d
) t
DECLARE @value1 NVARCHAR(MAX) = 'card 1 is : 4034349183539301 and the other one is 3456123485697865' DECLARE @Lenght INT ,@Count INT ,@Candidate CHAR ,@cNum INT ,@result VARCHAR(16) DECLARE @ value1 NVARCHAR(MAX)='卡片1为:4034349183539301,另一个为3456123485697865'DECLARE @长度INT,@ Count INT,@候选CHAR,@ cNum INT,@结果VARCHAR(16)
SELECT @Count = 1 SELECT @Count = 1
SELECT @cNum = 0 SELECT @cNum = 0
SELECT @result = '' SELECT @结果=''
SELECT @Lenght = LEN(@value1) SELECT @长度= LEN(@ value1)
WHILE @Count <= @Lenght BEGIN SELECT @Candidate = SUBSTRING(@value1, @Count, 1) @Count <= @长度开始选择@候选= SUBSTRING(@ value1,@Count,1)
IF @Candidate != ' '
AND ISNUMERIC(@Candidate) = 1
BEGIN
SET @cNum = @cNum + 1
SET @result = @result + @Candidate
END
ELSE
BEGIN
SET @cNum = 1
SET @result = ''
END
IF @cNum > 16
BEGIN
SELECT @result 'Credit Number'
END
SET @Count = @Count + 1
END 结束
There you go kind sir. 先生,你去那里。
DECLARE
@value3 NVARCHAR(MAX) = 'The payment1234567812345678is the place',
@MaxCount int,
@Count int,
@Numbers NVARCHAR(100)
SELECT @Count = 1
SELECT @Numbers = ''
SELECT @MaxCount = LEN(@value3)
WHILE @Count <= @MaxCount
BEGIN
IF (UNICODE(SUBSTRING(@value3,@Count,1)) >= 48 AND UNICODE(SUBSTRING(@value3,@Count,1)) <=57)
SELECT @Numbers = @Numbers + SUBSTRING(@value3,@Count,1)
SELECT @Count = @Count + 1
END
PRINT @Numbers
You can make this as a function if you are planning to use it a lot. 如果您打算大量使用它,可以将其作为功能使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.