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)
 initialize an int to the max length of a field

Author  Topic 

Cornelius19
Starting Member

30 Posts

Posted - 2008-08-05 : 11:32:57
Hi,

I would like to initialize an integer to the maximum length of a field. I tried the following:


create table tmp1 (word nvarchar (96))

Insert Into tmp1 Values('Blue')
Insert Into tmp1 Values('Yellow')
Insert Into tmp1 Values('Green')

declare @length integer
set @length = MAX(LEN(tmp1.word))


I hoped that @length would be initialized to 6 (since ‘Yellow’ is the longest and len('Yellow') = 6) but I got an error message:

Server: Msg 107, Level 16, State 3, Line 11
The column prefix 'tmp1' does not match with a table name or alias name used in the query.

Any idea how to make it work?

Cornelius


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-05 : 13:37:57
[code]select @length = MAX(t.WordLen)
FROM (SELECT LEN(tmp1.word) AS WordLen
FROM tmp1)t[/code]
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-05 : 23:12:50
[code]
select @length = MAX(LEN(tmp1.word))
from tmp1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Cornelius19
Starting Member

30 Posts

Posted - 2008-08-06 : 09:40:43
Thank you, both! It works.
Go to Top of Page
   

- Advertisement -