Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to find MAX and MIN value from declared variables in SQL using function.

How to find MAX and MIN value from declared variables in SQL using function.

Open SQL => Create function 
CREATE FUNCTION SplitString
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item int
)
AS
BEGIN
DECLARE@StartIndex INT,@EndIndex INT

SET@StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) @Character
BEGIN
SET @Input = @Input + @Character
END

WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)

INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex,@EndIndex - 1)

SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END

RETURN
END

Now you can find min and max values from below query 

SELECT max(Item),min(item)
FROM dbo.SplitString('14,2,111,905,67', ',')






This post first appeared on Asp.netSourceCodes, please read the originial post: here

Share the post

How to find MAX and MIN value from declared variables in SQL using function.

×

Subscribe to Asp.netsourcecodes

Get updates delivered right to your inbox!

Thank you for your subscription

×