I am new to the forum but have the following issue. I am trying to format
the output of a column using cast and varchar. Since the data is going to be
dynamic from day to day, I first determine the maximum size of the field and
then would like to use that size as the length value of the varchar command
so that the output field is consistent with that size. If I don't do this,
when I output the results, the output field is the size of the table field
(254). Below is the code snippet. How can I use the "@max_endpoint_name"
variable as the length for varchar? I know that the default length of
varchar using Cast is 30 but my data is going to be of variable length
between 30 and 254 and I would like the output to be of a fixed size as
specified with @max_endpoint_name so that data is not truncated or output in
hte max size.
declare @max_endpoint_name int
Select @max_endpoint_name = MAX(LEN(sOrigH323ID))
From calls
Select "Originating" = CAST(sOrigH323ID as varchar(@MAX_ENDPOINT_NAME))
FROM calls
If I substitute any digit for the @max_endpoint_name in the cast command it
works fine but the problem is that the value of max_endpoint_name is going to
be variable an my query needs to account for this.