Unix Technical Forum

Programming Field Lengths

This is a discussion on Programming Field Lengths within the SQL Server forums, part of the Microsoft SQL Server category; --> Is it possible to tell sql server to cast to a datatype and set the field length to a ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 09:07 AM
bozzzza@lycos.co.uk
 
Posts: n/a
Default Programming Field Lengths

Is it possible to tell sql server to cast to a datatype and set the
field length to a variable.

e.g. :-

declare @flen int
set @flen = 10

select (cast somefield as char(@flen) newfield)
into newtable
from sometable

I have also tried :-
select (cast somefield as char(max(len(somefield))) newfield)
into newtable
from sometable

When I try the above examples I get error in @flen; error in max
respectivly.

TIA

Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 09:07 AM
Simon Hayes
 
Posts: n/a
Default Re: Programming Field Lengths


<bozzzza@lycos.co.uk> wrote in message
news:1119438419.547258.218740@z14g2000cwz.googlegr oups.com...
> Is it possible to tell sql server to cast to a datatype and set the
> field length to a variable.
>
> e.g. :-
>
> declare @flen int
> set @flen = 10
>
> select (cast somefield as char(@flen) newfield)
> into newtable
> from sometable
>
> I have also tried :-
> select (cast somefield as char(max(len(somefield))) newfield)
> into newtable
> from sometable
>
> When I try the above examples I get error in @flen; error in max
> respectivly.
>
> TIA
>
> Simon
>


I don't believe there's any easy way to do this, but in most cases, it's
probably not necessary - instead of declaring char(10), why not just declare
varchar(1000), or whatever value is suitable for you? If you can explain why
you need to do this, someone may have a better solution. Depending on what
you need to achieve, you might be able to use dynamic SQL, but that has a
number of issues:

http://www.sommarskog.se/dynamic_sql.html

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 09:08 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Programming Field Lengths

(bozzzza@lycos.co.uk) writes:
> Is it possible to tell sql server to cast to a datatype and set the
> field length to a variable.
>
> e.g. :-
>
> declare @flen int
> set @flen = 10
>
> select (cast somefield as char(@flen) newfield)
> into newtable
> from sometable
>
> I have also tried :-
> select (cast somefield as char(max(len(somefield))) newfield)
> into newtable
> from sometable
>
> When I try the above examples I get error in @flen; error in max
> respectivly.


No, you would have to use dynamic SQL for that. Seems easier to use
varchar.

What do you want to achieve, really?


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 09:08 AM
bozzzza@lycos.co.uk
 
Posts: n/a
Default Re: Programming Field Lengths



Erland Sommarskog wrote:
> (bozzzza@lycos.co.uk) writes:
> > Is it possible to tell sql server to cast to a datatype and set the
> > field length to a variable.
> >
> > e.g. :-
> >
> > declare @flen int
> > set @flen = 10
> >
> > select (cast somefield as char(@flen) newfield)
> > into newtable
> > from sometable
> >
> > I have also tried :-
> > select (cast somefield as char(max(len(somefield))) newfield)
> > into newtable
> > from sometable
> >
> > When I try the above examples I get error in @flen; error in max
> > respectivly.

>
> No, you would have to use dynamic SQL for that. Seems easier to use
> varchar.
>
> What do you want to achieve, really?
>
>


Yhe problem is we have had some data supplied and the all the fields
lengths are set to 255 (nvarchar), even though this is not good pratice
we could live with it until someone else wanted a fixed length export
of the data.

So my idea was to work out the length of the fields and insert them as
the maximum width into the new table. Then the fixed length file would
look a lot better and cleaner.

Thanks for the reply, I will look into Dynamic SQL.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 09:08 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Programming Field Lengths

(bozzzza@lycos.co.uk) writes:
> Yhe problem is we have had some data supplied and the all the fields
> lengths are set to 255 (nvarchar), even though this is not good pratice
> we could live with it until someone else wanted a fixed length export
> of the data.
>
> So my idea was to work out the length of the fields and insert them as
> the maximum width into the new table. Then the fixed length file would
> look a lot better and cleaner.


Maybe. But what if the max lengths you find do agree with the actual
business rules? Next time you get a refresh, you could get an error
because of truncation.

So I would suggest that either you find out the actual max lengths, or
you leave the table the way it is.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 09:08 AM
David Portas
 
Posts: n/a
Default Re: Programming Field Lengths

You have asked the same question in
microsoft.public.sqlserver.programming. Please don't post the same
question independently to diffferent groups. It's inconsiderate to
others who may waste time responding on something that has already been
answered elsewhere.

In your other thread you indicated that your intention is to
standardize the column sizes for reporting purposes. All the reporting
tools I know of allow you to specify a field width shorter than the
actual column width so I'm not sure why you would want to do this in
SQL. Keep it in the presentation tier is my suggestion.

--
David Portas
SQL Server MVP
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 09:08 AM
bozzzza@lycos.co.uk
 
Posts: n/a
Default Re: Programming Field Lengths



David Portas wrote:
> You have asked the same question in
> microsoft.public.sqlserver.programming. Please don't post the same
> question independently to diffferent groups. It's inconsiderate to
> others who may waste time responding on something that has already been
> answered elsewhere.


Sorry.

> In your other thread you indicated that your intention is to
> standardize the column sizes for reporting purposes. All the reporting
> tools I know of allow you to specify a field width shorter than the
> actual column width so I'm not sure why you would want to do this in
> SQL. Keep it in the presentation tier is my suggestion.
>


Actually I needed to create a fix length text file of the data, so a
pascal programmer could import it into a DOS application, and the
programmer wasn't happy that the fields were coming out at 255 each.

After reading Erland's post, I gave the programmer the export in Comma
delimited format instead, so a refresh of the data won't effect the
export.

But thanks to all the posts I now know dynamic sql exists (I thought
exec was just for stored procedures) and it has opened up a whole new
world for me.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 09:08 AM
debian mojo
 
Posts: n/a
Default Re: Programming Field Lengths

Yes it is possible!

Do it this way!

eg :-

declare @flen int
set @flen = 10

exec('select cast(somefield as char(' + @flen + ')) as newfield into
newtable
from oldtable')


Regards
Debian

*** Sent via Developersdex http://www.developersdex.com ***
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 09:08 AM
David Portas
 
Posts: n/a
Default Re: Programming Field Lengths

> I now know dynamic sql exists (I thought
> exec was just for stored procedures) and it has opened up a whole new
> world for me.


Make sure you understand the implications. Dynamic SQL should usually
be a last resort in production code. See:
http://www.sommarskog.se/dynamic_sql.html

--
David Portas
SQL Server MVP
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:46 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com