Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Retrieving a MAX value out of a ROW of fields

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-10-26 : 08:00:58
Nick writes "Hi,

I am trying to find a function in SQL Server that replicates the Excel command MAXA.
This command will give the Greatest value out of a comma-seperated series of values or fields.

So if I have the SQL code:
Select 4, 5, 2, 7, 3, 1

I would like a function to return 7.

Thanks
Regards
Nick"

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-10-26 : 08:21:41
Somthing like this ??

Create Function GetMax
(
@sNumber varchar(8000),
@sSeperator char(1) = ','
)
Returns float
As
Begin

Declare @MaxNumber Float
if isnull(@sSeperator,'') = ''
Set @sSeperator = ','

Select @MaxNumber = Max(INROW)
From
(
Select NullIf(SubString(@sSeperator + @sNumber + @sSeperator , IDNos ,
CharIndex(@sSeperator , @sSeperator + @sNumber + @sSeperator , IDNos) - IDNos) , '') AS INROW
FROM
(
SELECT top 100 percent a.i+b.i+c.i+d.i as IDNos FROM
(
Select 0 i union all Select 1 union all Select 2 union all Select 3 union all Select 4 union all Select 5 union all Select 6 Union All
Select 7 union all Select 8 union all Select 9 union all Select 10 union all Select 11 union all Select 12 Union All
Select 13 union all Select 14 union all Select 15
) as a,
(
Select 0 i union all Select 16 union all Select 32 union all Select 48 union all Select 64 union all Select 80 union all Select 96 Union All
Select 112 union all Select 128 union all Select 144 union all Select 160 union all Select 176 union all Select 192 Union All
Select 208 union all Select 224 union all Select 240
) as b,
(
Select 0 i union all Select 256 union all Select 512 union all Select 768 union all Select 1024 union all Select 1280 union all Select 1536 Union All
Select 1792 union all Select 2048 union all Select 2304 union all Select 2560 union all Select 2816 union all Select 3072 Union All
Select 3328 union all Select 3584 union all Select 3840
) as c,
(
Select 0 i union all Select 4096
) as d
order by 1
) as f
WHERE IDNos <= Len(@sSeperator + @sNumber + @sSeperator) AND
SubString(@sSeperator + @sNumber + @sSeperator , IDNos - 1, 1) = @sSeperator
AND CharIndex(@sSeperator , @sSeperator + @sNumber + @sSeperator , IDNos) - IDNos > 0
) as f
Where IsNumeric(InRow) =1

return @MaxNumber

End

GO

Select Dbo.GetMax('4,5,2,7,3,1',',')


Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-26 : 10:12:38
Also refer
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-26 : 10:25:03
[code]select max(numberval)
from CSVTable('4,5,2,7,3,1')[/code]

refer to CSVTable funciton here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable


KH

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-10-26 : 11:17:03
Yeah, that was a good thread...I should bookmark it



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -