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
 Transact-SQL (2000)
 MAX function

Author  Topic 

tommynz
Starting Member

9 Posts

Posted - 2004-08-17 : 04:46:28
Is it possible to find the MAX of two variables (rather than the MAX value in a column)?

For example

DECLARE @V1 int,
@V2 int,
@V3 int

SET @V1 = 1
SET @V2 = 2

SET @V3 = MAX(@V1, @V2) --??? I know this is wrong

What should the syntax be? I have searched in Books Online and on the net and not found an example of this.
Can anyone help?

Many Thanks
Michael


ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-08-17 : 05:10:06
YOU Can Use CASE:

DECLARE @V1 INT, @V2 INT, @V3 INT

SET @V1=1
SET @V2=2

SET @V3 = (SELECT CASE WHEN @V1 > @V2 THEN @V1 ELSE @V2 END)


Duane.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-08-17 : 06:58:07
MAX(@V1, @V2, @V3, ...) would be good, wouldn't it!

Kristen
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-08-17 : 07:34:00
No, doesn't look like it:

[CODE]
declare @v1 int
declare @v2 int

set @v1 = 1
set @v2 = 2

select max(@v1, @v2)
[/CODE]

Server: Msg 174, Level 15, State 1, Line 7
The max function requires 1 arguments.


Duane.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-08-17 : 08:01:40
Yeah, I know. I was dreaming ....

SELECT MAX(foo.bar)
FROM
(
SELECT @v1 AS bar
UNION ALL SELECT @v2
UNION ALL SELECT @v3
) foo

looks so ugly, don't you think?

Kristen
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-08-17 : 08:13:35
Yeah!
F U B A R

Duane.
Go to Top of Page

tommynz
Starting Member

9 Posts

Posted - 2004-08-18 : 03:29:07
Great, thank you for your help Kristen and Duane

Michael
Go to Top of Page
   

- Advertisement -