if object_id('NthChar','FN') is not null
drop function Nthchar
GO
create function NthChar
(
@source_string as nvarchar(4000),
@sub_string as nvarchar(1024),
@nth as int
)
returns int
as
begin
declare @postion int
declare @count int
set @postion = CHARINDEX(@sub_string, @source_string)
set @count = 0
while @postion > 0
begin
set @count = @count + 1
if @count = @nth
begin
break
end
set @postion = CHARINDEX(@sub_string, @source_string, @postion + 1)
End
return @postion
end
GO
--select dbo.NthChar('abcabc','abc',2)
--4
2. 通过CTE,对待处理的整个表字段操作, 递归中每次为charindex加一个计数,直到为N
if object_id('tempdb..#T') is not null
drop table #T
create table #T
(
source_string nvarchar(4000)
)
insert into #T values (N'我们我们')
insert into #T values (N'我我哦我')
declare @sub_string nvarchar(1024)
declare @nth int
set @sub_string = N'我们'
set @nth = 2
;with T(source_string, starts, pos, nth)
as (
select source_string, 1, charindex(@sub_string, source_string), 1 from #t
union all
select source_string, pos + 1, charindex(@sub_string, source_string, pos + 1), nth+1 from T
where pos > 0
)
select
source_string, pos, nth
from T
where pos <> 0
and nth = @nth
order by source_string, starts
--source_string pos nth
--我们我们 3 2
--numbers/tally table
IF EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].[Numbers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROP TABLE dbo.Numbers
--===== Create and populate the Tally table on the fly
SELECT TOP 1000000
IDENTITY(int,1,1) AS number
INTO dbo.Numbers
FROM master.dbo.syscolumns sc1,
master.dbo.syscolumns sc2
--===== Add a Primary Key to maximize performance
ALTER TABLE dbo.Numbers
ADD CONSTRAINT PK_numbers_number PRIMARY KEY CLUSTERED (number)
--===== Allow the general public to use it
GRANT SELECT ON dbo.Numbers TO PUBLIC
--以上数字表创建一次即可,不需要每次都重复创建
DECLARE @source_string nvarchar(4000),
@sub_string nvarchar(1024),
@nth int
SET @source_string = 'abcabcvvvvabc'
SET @sub_string = 'abc'
SET @nth = 2
;WITH T
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY number) AS nth,
number AS [Position In String]
FROM dbo.Numbers n
WHERE n.number <= LEN(@source_string)
AND CHARINDEX(@sub_string, @source_string, n.number)-number = 0
----OR
--AND SUBSTRING(@source_string,number,LEN(@sub_string)) = @sub_string
)
SELECT * FROM T WHERE nth = @nth
select instr('abcdefghijkabc','abc', 1, 2) position from dual;
(3) 多个相同字符连续,合并为一个字符
select regexp_replace(trim('agc f f '),'\s+',' ') from dual;
(4) 是否为有效IP/身份证号/手机号等
--是否为有效IP
WITH IP
AS(
SELECT '10.20.30.40' ip_address FROM dual UNION ALL
SELECT 'a.b.c.d' ip_address FROM dual UNION ALL
SELECT '256.123.0.254' ip_address FROM dual UNION ALL
SELECT '255.255.255.255' ip_address FROM dual
)
SELECT *
FROM IP
WHERE REGEXP_LIKE(ip_address, '^(([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$');
--是否为有效身份证/手机号,暂未举例
2. SQL Server
目前最新版本为SQL Server 2017,还没有对REGEXP函数的支持,需要通用CLR来扩展,如下为CLR实现REG_REPLACE:
--1. 开启 CLR
EXEC sp_configure 'show advanced options' , '1'
GO
RECONFIGURE
GO
EXEC sp_configure 'clr enabled' , '1'
GO
RECONFIGURE
GO
EXEC sp_configure 'show advanced options' , '0';
GO
2. 创建 Assembly
--3. 创建 CLR 函数
CREATE FUNCTION [dbo].[regex_replace](@input [nvarchar](4000), @pattern [nvarchar](4000), @replacement [nvarchar](4000))
RETURNS [nvarchar](4000) WITH EXECUTE AS CALLER, RETURNS NULL ON NULL INPUT
AS
EXTERNAL NAME [RegexUtility].[RegexUtility].[RegexReplaceDefault]
GO
--4. 使用regex_replace替换多个空格为一个空格
select dbo.regex_replace('agc f f ','\s+',' ');