一、效果

二、定义
USE [你的数据库名];
GO
/****** Object: UserDefinedFunction [dbo].[IsEnglish] Script Date: 2022/6/9 14:52:41 ******/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- =============================================
-- Author: feiyuit
-- Create date: 2022-6-9 14:53:01
-- Description: 匹配指定内容是否纯英文字母(不区分大小写),纯英文返回1,否则返回0
-- =============================================
ALTER FUNCTION [dbo].[IsEnglish]
(
-- Add the parameters for the function here
@@text NVARCHAR(255)
)
RETURNS NVARCHAR(255)
AS
BEGIN
-- 实现参考:
-- https://docs.microsoft.com/zh-cn/sql/t-sql/language-elements/string-operators-transact-sql?view=sql-server-2017
-- https://docs.microsoft.com/zh-cn/sql/t-sql/functions/patindex-transact-sql?view=sql-server-2017&WT.mc_id=DT-MVP-5003010
-- Return the result of the function
RETURN CASE @@text
WHEN '' THEN
0
ELSE
CASE PATINDEX('%[^A-Za-z]%', @@text)
WHEN 0 THEN
1
ELSE
0
END
END;
END;
GO
三、使用
SELECT dbo.IsEnglish(NULL),
dbo.IsEnglish(''),
dbo.IsEnglish('helloword'),
dbo.IsEnglish('hello word'),
dbo.IsEnglish('hello123456')