Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
using variable length to cast a varchar
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
el_ortega  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 18:13
Newsgroups: microsoft.public.sqlserver.programming
From: el_ortega <elort...@discussions.microsoft.com>
Date: Thu, 5 Nov 2009 10:13:02 -0800
Local: Thurs 5 Nov 2009 18:13
Subject: using variable length to cast a varchar
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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Cooper  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 18:28
Newsgroups: microsoft.public.sqlserver.programming
From: "Tom Cooper" <tomcoo...@comcast.net>
Date: Thu, 5 Nov 2009 13:28:42 -0500
Local: Thurs 5 Nov 2009 18:28
Subject: Re: using variable length to cast a varchar
To do this, you will need dynamic SQL.  See
http://www.sommarskog.se/dynamic_sql.html for a good introduction to that
topic.

Tom

"el_ortega" <elort...@discussions.microsoft.com> wrote in message

news:92FD8AF1-EA2A-43B6-99E5-24B9099A9C55@microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Isaacs  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 19:51
Newsgroups: microsoft.public.sqlserver.programming
From: Eric Isaacs <eisa...@gmail.com>
Date: Thu, 5 Nov 2009 11:51:57 -0800 (PST)
Local: Thurs 5 Nov 2009 19:51
Subject: Re: using variable length to cast a varchar
Why not just use VARCHAR(254)?  VARCHAR is dynamic.  If the length is
30, just that number of characters will be stored.  If you want it to
be fixed length, you should be using CHAR(size) not VARCHAR(size) but
then if you set it to CHAR(50) and the data in one row is only 30,
you'll get your 30 characters followed by 20 spaces.  From what you
described, VARCHAR(254) seems like the solution.

-Eric Isaacs


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google