日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

比较第一与第二个字符串,是否有连续的5个字符相同.sql

發(fā)布時(shí)間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 比较第一与第二个字符串,是否有连续的5个字符相同.sql 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
--比較第一與第二個(gè)字符串,是否有連續(xù)的5個(gè)字符相同,如果有,返回1,否則返回0

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_compstr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_compstr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[序數(shù)表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序數(shù)表]
GO

--為提高效率,最好創(chuàng)建一個(gè)固定的序數(shù)表
select top 1600 id=identity(int,1,1) into 序數(shù)表 from sysobjects a,sysobjects b

go
create function f_compstr(
@str1 varchar(8000),
@str2 varchar(8000)
) returns bit
as
begin
?? ?declare @re bit
?? ?/*--如果使用動(dòng)態(tài)序數(shù),效率極低.
?? ?declare @tb table(id int identity(1,1),a int)
?? ?insert into @tb(a) select top 1600 null from sysobjects a,sysobjects b
?? ?--*/
?? ?
?? ?if len(@str1)>len(@str2) set @re=case when exists(
?? ??? ?select 1
?? ??? ?from (select name=@str2)a,序數(shù)表 b? --使用動(dòng)態(tài)序數(shù),改為: ,@tb b
?? ??? ?where b.id<=len(@str2)-4 and @str1 like '%'+substring(name,b.id,5)+'%'
?? ??? ?) then 1 else 0 end
?? ?else set @re=case when exists(
?? ??? ?select 1
?? ??? ?from (select name=@str1)a,序數(shù)表 b? --使用動(dòng)態(tài)序數(shù),改為: ,@tb b
?? ??? ?where b.id<=len(@str1)-4 and @str2 like '%'+substring(name,b.id,5)+'%'
?? ??? ?) then 1 else 0 end
?? ?return(@re)
end
go

--調(diào)用示例
select dbo.f_compstr('浦東:田園小區(qū)148號(hào)405室','浦:田園小區(qū)148幢405室')
go

--使用例子,查詢(xún)不重復(fù)的記錄,重復(fù)的,只顯示第一條:
declare @t table(id int identity(1,1),address varchar(1000))
insert into @t
select '浦東:田園小區(qū)148號(hào)405室'
union all select '浦:田園小區(qū)148幢405室'
union all select '浦:田園小區(qū)148幢405室'
union all select '浦:田園小區(qū)148幢405室'
union all select 'abcdefghijklmn'
union all select 'abcdefg'
union all select 'abcdefghijklmn'
union all select 'abcdefg'
union all select '123456768898'
union all select '123345'

select * from @t a
where id=(select min(id) from @t where dbo.f_compstr(a.address,address)=1)

/*--測(cè)試結(jié)果

id????????? address??????????????????? ?
----------- ----------------------------
1?????????? 浦東:田園小區(qū)148號(hào)405室
5?????????? abcdefghijklmn
9?????????? 123456768898
10????????? 123345
--*/

總結(jié)

以上是生活随笔為你收集整理的比较第一与第二个字符串,是否有连续的5个字符相同.sql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。