SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Width of column


Width of column

Author
Message
sree-226516
sree-226516
SSC Eights!
SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)

Group: General Forum Members
Points: 845 Visits: 609
Is it possible to change the width of column in sql server?



Thanks,

SR

Thanks,
SR
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5992 Visits: 4550
Yes. If you want to lengthen a column, use ALTER TABLE to assign new data length. If you want to shorten the column, you'll want to drop/add the table or add the new column, copy the data, drop the existing column and sp_rename the column.

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
sree-226516
sree-226516
SSC Eights!
SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)SSC Eights! (845 reputation)

Group: General Forum Members
Points: 845 Visits: 609
Thanks John.



SR

Thanks,
SR
Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28769 Visits: 9671
CREATE TABLE #Test (a VARCHAR(30))

INSERT INTO #Test (a) SELECT REPLICATE('A', 29)

ALTER TABLE #Test

ALTER COLUMN a VARCHAR (29)

--this works fine



DROP TABLE #Test

GO





CREATE TABLE #Test (a VARCHAR(30))

INSERT INTO #Test (a) SELECT REPLICATE('A', 30)

ALTER TABLE #Test

ALTER COLUMN a VARCHAR (29)

--Throws error message because it would change the data

GO

--You can figure out which rows will return an error with a query like this one :

--SELECT * WHERE Column <> Convert(NewDataType, Column)

SELECT * FROM #Test WHERE a <> CONVERT(VARCHAR(29), a)



DROP TABLE #Test
homebrew01
homebrew01
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4793 Visits: 9108
Or you can do it the easy way with the GUI and get it done in about 4 clicks.



Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28769 Visits: 9671
There's always that best practice of scripting everything, but you can always ignore it if you want. Wink
homebrew01
homebrew01
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4793 Visits: 9108
By Best Practices, do you mean make the change with the script, or save the script for historical purposes ?



When I make changes with the GUI, I have set my default to prompt me automatically to generate a script, so I can archive it as I do for SP and other changes.



Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28769 Visits: 9671
I had ignored that option when I started to manually script things... maybe it's time to start to use other options Wink.



Thanks for the idea!





It's also important to point out that I don't really like the way EM scripts things so maybe that's why I was always relunctant to use it... It's definitly a good learning experience however!
James Horsley
James Horsley
SSC Veteran
SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)

Group: General Forum Members
Points: 275 Visits: 451
It is good to have the option on to generate scripts anyway (however much you like/dislike the SQL produced) - as this reminds you that you should have scripted!



Pity the option is so weirdly hidden in Enterprise Manager - what were they thinking.



For those reading this thread who are unaware of this option - you can set SQL Enterprise Manager to always prompt you to save a change script when you change an object through the GUI - only way I have found to set this on in the GUI is make a change to an object (e.g. a table) and while you are doing it there is a little icon in the tool bar that looks like a scroll with a floppy disk in front of it - clicking this allows you to create a change script and on the bottom of the save script box there is a tick box to "Automatically generate change script on every save" - tick that and in future you will be presented with a create change option every time you change something (you can always answer no to the script option if you don't want one in future - but the reminder will be there).



For those happy with RegEdit the option actually ends up in



HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\80\Tools\SQLEW\Datatools as AutoSaveChangeScript dword 1 = do 0 = don't



Take the likes below into a text file - give it a .reg extension and you have a .reg file you can apply wherever you want this set:



Windows Registry Editor Version 5.00



[HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\80\Tools\SQLEW\Datatools]

"AutoSaveChangeScript"=dword:00000001




James Horsley
Workflow Consulting Limited
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search