你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / 数据库开发
sqlserver判断是否重复值
 
一个字符串为“1;2;1;1;1”。
请问:
如何在SQL SERVER中实现如下功能:
1、判断该字符串中是否存在重复的数字
如果存在重复的数字,返回错误信息
如果不存在重复的数字,返回正确。


思路:分割字符串,将子字符串存入临时表,在临时表进行分组判断

create function RepeatString(@input varchar(8000),@separator varchar(10))
returns  int
as
 begin    
    declare @temp table(part varchar(100)) 
	 declare @i int ,@result int
	 set @input=rtrim(ltrim(@input))    
	 set @i=charindex(@separator,@input)    
	 while @i>=1    
	 begin        
			 insert @temp values(left(@input,@i-1))        
			 set @input=substring(@input,@i+1,len(@input)-@i)        
			 set @i=charindex(@separator,@input)    
	 end    
	 if exists(select part,count(*) from @temp group by part having count(*)>1)
	   set @result=1 --存在重复
	   else 
	   set @result=0 --不存在重复
	   return   @result
end
go

--测试

select  dbo.RepeatString('1,1,2,3,1',',')  --1
select  dbo.RepeatString('1,2,3,4,5',',')  --0

  推荐精品文章

·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 
·2023年7月目录
·2023年6月目录 
·2023年5月目录
·2023年4月目录 
·2023年3月目录 
·2023年2月目录 
·2023年1月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089