-
Using a declare variable to represent a data type length
I'm trying to use a variable set by DECLARE to represent a data type length with CAST. Don't ask why. Here is what I'm got:
declare @n int
set @n = (select max(len(col_1)) from tblOne)
select cast(Col_2 as varchar(@n))
from tblOne
This should make Col_2 the length of the longest entry in Col_1. It does not work. I must use a set number for the length after the data type like this:
declare @n int
set @n = (select max(len(col_1)) from tblOne)
select cast(Col_2 as varchar(28)) -- (28 is longest current entry in Col_1)
from tblOne
Is there a way around this? I'm new to SQL.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|