集合中的最大值,最小值在收集和集合中的值的数目。
/*
例:
运行下面的函数
select* from dbo.GetMaxandMinValue('2,22,5,1,88,2000,7897,800',',')
得到这样的结果
minValue maxValue CountOfNumberSupplied
-------------------------------------------------------------------
1.00 7897.00 8
*/
if object_id('GetMaxandMinValue','F')is not null drop functionGetMaxandMinValue
go
createfunctionGetMaxandMinValue(@valuescollection varchar(max),@separatorchar(1))
returns@table table(minvalue numeric(20,2),maxvaluenumeric(20,2),countofnumbersuppliedint)
as
begin
declare@x xml
declare@minnumber varchar(50)
declare@maxnumber varchar(50)
declare@numbercount varchar(20)
select@x='<number>'+ replace(@valuescollection,@separator,'</number><number>')+'</number>'
select@minnumber= convert(varchar(50),@x.query('fn:min(number)'))
select@maxnumber= convert(varchar(50),@x.query('fn:max(number)'))
select@numbercount= convert(varchar(50),@x.query('fn:count(number)'))
insertinto @table select cast(@minnumberas float),cast(@maxnumberas float),cast(@numbercountas int)
return;
end
go
select* from dbo.GetMaxandMinValue('2,22,5,1,88,2000,7897,800',',')