SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 reducing the size of a column in sql table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  15:19:33  Show Profile  Reply with Quote
I currently have a sql table which we receive from the vendor that houses over 800 fields. The vendor provides us with the maximum size of each field, even though our company may not utilize the entire length for that specific field. For Example:

dbo.CustData

Instrument_ID (nvarchar(40),null)
Term_maturity (numeric(8,0),null)
Payment_amt (numeric(18,3),null)
.
.
.

I would like to rewrite this table so that each field is smaller. I was utilizing the ALTER TABLE ALTER COLUMN commands and it works fine when I increase/decrease the size of my numeric fields. I just get an error when I try to reduce the size of my
nvarchar field. The error basically states "String or binary data would be truncated. The statement has been terminated". Is there a way around this. Below is the query I created:

ALTER TABLE CustData ALTER COLUMN Instrument_ID nvarchar(20),null;
ALTER TABLE CustData ALTER COLUMN Term_maturity numeric(10,0), null;
ALTER TABLE CustData ALTER COLUMN Payment_amt numeric(15,5),null;

Any suggestions or better way of doing this would be greatly appreciated it.

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/20/2012 :  15:22:07  Show Profile  Reply with Quote
that means you already have some data in field which is having length above the one you're trying to set it
so first check that and make sure you set it having length enough to hold current data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  15:36:17  Show Profile  Reply with Quote
Thanks Visakh16,
So what you are telling me, is that basically it will never make the field smaller if it truncates values. Is there a way around this, or I have no choice?

Also, can you tell me, is there a way to have one Alter Table statement when modifying mutliple columns? If yes, would you mind providing me an example? thanks again
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3821 Posts

Posted - 03/20/2012 :  15:41:57  Show Profile  Reply with Quote
quote:
Originally posted by cirugio

Thanks Visakh16,
So what you are telling me, is that basically it will never make the field smaller if it truncates values. Is there a way around this, or I have no choice?

Also, can you tell me, is there a way to have one Alter Table statement when modifying mutliple columns? If yes, would you mind providing me an example? thanks again

If you want to reduce the size of column that already has data larger than you want to make it you need to update the column first with the "truncated" value; then alter the column.

Edited by - Lamprey on 03/20/2012 15:42:30
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/20/2012 :  15:42:33  Show Profile  Reply with Quote
it it already have values it wont truncate automatically. you've to first apply update logic to truncate it and then do ALTER ....COLUMN

also ALTER TABLE.... ALTER COLUMN ... has to be repeated for each column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  15:43:43  Show Profile  Reply with Quote
How about if I just want to truncate whats already in there?
Go to Top of Page

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  15:52:03  Show Profile  Reply with Quote
Visakh16, can you provide me with an example of how I would update it with the truncated value. Sorry, I am a bit unclear.
For instance:

. if the field I want to truncate is: Instrument_id nvarchar(40) and contains the value of 'John Smith 445 N. Denvor Street, Colardo'
. and I want to it to truncate to 30 characters so that the field would contain 'John Smith 445 N. Denvor Stree' without specifically typing in the values in the update statement.

how would I use the update function ? Would I have to use a substring in the update? Sorry, I am fairly new to sql, so have a little learning curve. Thanks again.

Edited by - cirugio on 03/20/2012 15:54:48
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 03/20/2012 :  15:56:52  Show Profile  Reply with Quote
I would NOT TOUCH anything from the Vendor. You can void any service agreement you may have or violate some other contractual arrangement...

BUT, Just like every SCRUB Vendor I've seen...a table with 800 columns...every product I've seen is GARBAGE



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/20/2012 :  15:58:48  Show Profile  Reply with Quote
quote:
Originally posted by cirugio

Visakh16, can you provide me with an example of how I would update it with the truncated value. Sorry, I am a bit unclear.
For instance:

. if the field I want to truncate is: Instrument_id nvarchar(40) and contains the value of 'John Smith 445 N. Denvor Street, Colardo'
. and I want to it to truncate to 30 characters so that the field would contain 'John Smith 445 N. Denvor Stree' without specifically typing in the values in the update statement.

how would I use the update function ? Would I have to use a substring in the update? Sorry, I am fairly new to sql, so have a little learning curve. Thanks again.


you would do it like

UPDATE table
SET Column=LEFT(Column,30)


but why should you do it that way
are you aware that this will cause of loss of information. I wont do that unless I'm sure that whatever data it had was junk or had no relevance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  16:03:28  Show Profile  Reply with Quote
Perfect! Thank you Visakh16.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/20/2012 :  16:06:12  Show Profile  Reply with Quote
quote:
Originally posted by cirugio

Perfect! Thank you Visakh16.


you didnt answer my question
are you aware of consequences??

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cirugio
Yak Posting Veteran

88 Posts

Posted - 03/20/2012 :  16:46:46  Show Profile  Reply with Quote
Yes, I am aware of the consequences. The data that is in the tables is basically garbage test data that was not cleared out from when the applications was in UAT. The fields which I would be working with are just user fields and do not relate to production data. My changes will not impact anything. Thank you for validating.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/20/2012 :  16:59:10  Show Profile  Reply with Quote
ok...then fine with that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000