經過2個函數CHARINDEX和PATINDEX以及通配符的靈活使用
函數:CHARINDEX和PATINDEX
CHARINDEX:查某字符(串)是否包含在其餘字符串中,返回字符串中指定表達式的起始位置。
PATINDEX:查某字符(串)是否包含在其餘字符串中,返回指定表達式中某模式第一次出現的起始位置;若是在所有有效的文本和字符數據類型中沒有找到該模式,則返回零。特殊:能夠使用通配符!html
例子:
1. 查詢字符串中是否包含非數字字符
SELECT PATINDEX('%[^0-9]%', '1235X461')
SELECT PATINDEX('%[^0-9]%', '12350461')函數
2. 查詢字符串中是否包含數字字符
SELECT PATINDEX('%[0-9]%', 'SUYLLGoO')
SELECT PATINDEX('%[0-9]%', 'SUYLLG0O')post
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
ENDurl
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
ENDhtm
5. 函數判斷字符串不包含任何符號(包括空格)
CREATE FUNCTION [dbo].fn_IsAlphanumeric
(
@pString VARCHAR(8000)
)
RETURNS bit
WITH ENCRYPTION
AS
BEGIN
DECLARE @vJudge int
SET @vJudge = 0
SELECT @vJudge =
CASE
WHEN PATINDEX('%[^a-z0-9]%', LOWER(@pString)) > 0 THEN 0
WHEN PATINDEX('%[^a-z0-9]%', LOWER(@pString)) = 0 THEN 1
END
RETURN @vJudge
ENDblog
6. 函數判斷字符串不包含任何符號(除空格外)
CREATE FUNCTION [dbo].fn_IsAlphanumericBlank
(
@pString VARCHAR(8000)
)
RETURNS bit
WITH ENCRYPTION
AS
BEGIN
DECLARE @vJudge int
SET @vJudge = 0
SELECT @vJudge =
CASE
WHEN PATINDEX('%[^a-z0-9 ]%', LOWER(@pString)) > 0 THEN 0
WHEN PATINDEX('%[^a-z0-9 ]%', LOWER(@pString)) = 0 THEN 1
END
RETURN @vJudge
END
-- 注意:[^a-z0-9 ]模式中最後有一個空格。字符串
用charindex()——charindex(字符,字符串)>0 –>包含get