今天給大家介紹了SQL Server 提取數字、提取英文、提取中文語句 ,需要的朋友可以參考下,希望可以幫助到大家。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
--SQL 判斷字段值是否有中文
create
function
fun_getCN(@str nvarchar(4000))
returns
nvarchar(4000)
as
begin
declare
@word
nchar
(1),@CN nvarchar(4000)
set
@CN=
''
while len(@str)>0
begin
set
@word=
left
(@str,1)
if unicode(@word)
between
19968
and
19968+20901
set
@CN=@CN+@word
set
@str=
right
(@str,len(@str)-1)
end
return
@CN
end
select
dbo.fun_getCN(
'ASDKG論壇KDL'
)
--論壇
select
dbo.fun_getCN(
'ASDKG論壇KDL'
)
--論壇
select
dbo.fun_getCN(
'ASDKDL'
)
--空
-------------------------------------
--提取數字
IF OBJECT_ID(
'DBO.GET_NUMBER2'
)
IS
NOT
NULL
DROP
FUNCTION
DBO.GET_NUMBER2
GO
CREATE
FUNCTION
DBO.GET_NUMBER2(@S
VARCHAR
(100))
RETURNS
VARCHAR
(100)
AS
BEGIN
WHILE PATINDEX(
'%[^0-9]%'
,@S) > 0
BEGIN
set
@s=stuff(@s,patindex(
'%[^0-9]%'
,@s),1,
''
)
END
RETURN
@S
END
GO
--測試
PRINT DBO.GET_NUMBER(
'呵呵ABC123ABC'
)
GO
--123
--------------------------------------------------------------------
--提取英文
IF OBJECT_ID(
'DBO.GET_STR'
)
IS
NOT
NULL
DROP
FUNCTION
DBO.GET_STR
GO
CREATE
FUNCTION
DBO.GET_STR(@S
VARCHAR
(100))
RETURNS
VARCHAR
(100)
AS
BEGIN
WHILE PATINDEX(
'%[^a-z]%'
,@S) > 0
BEGIN
set
@s=stuff(@s,patindex(
'%[^a-z]%'
,@s),1,
''
)
END
RETURN
@S
END
GO
--測試
PRINT DBO.GET_STR(
'呵呵ABC123ABC'
)
GO
--------------------------------------------------------------------
--提取中文
IF OBJECT_ID(
'DBO.CHINA_STR'
)
IS
NOT
NULL
DROP
FUNCTION
DBO.CHINA_STR
GO
CREATE
FUNCTION
DBO.CHINA_STR(@S NVARCHAR(100))
RETURNS
VARCHAR
(100)
AS
BEGIN
WHILE PATINDEX(
'%[^吖-座]%'
,@S) > 0