Home > Database > Mysql Tutorial > 判断字符串中包含子字符串的方法

判断字符串中包含子字符串的方法

WBOY
Release: 2016-06-07 14:57:24
Original
1620 people have browsed it

通过2个函数CHARINDEX和PATINDEX以及通配符的灵活使用,判断字符串中包含子字符串的方法 无 源码与演示: 源码出处 1. 查询字符串中是否包含非数字字符SELECT PATINDEX('%[^0-9]%', '1235X461')SELECT PATINDEX('%[^0-9]%', '12350461')2. 查询字符串中是否包

通过2个函数 CHARINDEX 和 PATINDEX 以及通配符的灵活使用,判断字符串中包含子字符串的方法

源码与演示:源码出处

1. 查询字符串中是否包含非数字字符
SELECT PATINDEX('%[^0-9]%', '1235X461')
SELECT PATINDEX('%[^0-9]%', '12350461')

2. 查询字符串中是否包含数字字符
SELECT PATINDEX('%[0-9]%', 'SUYLLGoO')
SELECT PATINDEX('%[0-9]%', 'SUYLLG0O')

3.函数判断字符串只包含数字
CREATE FUNCTION [dbo].fn_IsNumeric
(
@pString VARCHAR(8000)
)
RETURNS bit
WITH ENCRYPTION
AS
BEGIN
DECLARE @vJudge int
SET @vJudge = 0
SELECT @vJudge =
CASE
WHEN PATINDEX('%[0-9]%', LOWER(@pString)) > 0 THEN 0
WHEN PATINDEX('%[0-9]%', LOWER(@pString)) = 0 THEN 1
END
RETURN @vJudge
END

4.函数判断字符串只包含字母(忽略大小写)
CREATE FUNCTION [dbo].fn_IsAlpha
(
@pString VARCHAR(8000)
)
RETURNS bit
WITH ENCRYPTION
AS
BEGIN
DECLARE @vJudge int
SET @vJudge = 0
SELECT @vJudge =
CASE
WHEN PATINDEX('%[a-z]%', LOWER(@pString)) > 0 THEN 0
WHEN PATINDEX('%[a-z]%', LOWER(@pString)) = 0 THEN 1
END
RETURN @vJudge
END
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template