{"id":18735,"date":"2022-06-30T13:45:33","date_gmt":"2022-06-30T11:45:33","guid":{"rendered":"https:\/\/expert-only.com\/?p=18735"},"modified":"2022-11-29T11:36:24","modified_gmt":"2022-11-29T10:36:24","slug":"modify-a-sql-server-column-with-a-script","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/modify-a-sql-server-column-with-a-script\/","title":{"rendered":"Modify a SQL Server column with a script"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\" id=\"h-how-to-modify-a-column-in-a-sql-server-table-with-a-script\"><strong><em>How to modify a column in a SQL Server table with a script?<\/em><\/strong><\/h4>\n\n\n\n<p>Tutorial to modify a SQL Server column with a T-SQL ALTER TABLE and ALTER COLUMN command. This SQL code allows you to modify the structure of a table. The table is created in the first part with a length of 20 characters. This already existing column is changed to a length of 100 characters.<\/p>\n\n\n\n<p>The example used is the Sales table created in this CREATE TABLE SQL script example. Create the table first before changing the column. Connect to the target instance and <a href=\"https:\/\/aws.amazon.com\/fr\/getting-started\/hands-on\/create-microsoft-sql-db\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">database<\/a> with SSMS and execute the code below in a new window.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Create the example table to store the Sales data\nCREATE TABLE [dbo].[Sales] (\n  [MonthName] NVARCHAR(20),\n  [Amount] NUMERIC(5)\n);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">T-SQL script to change the length of a column<\/h2>\n\n\n\n<p>In effect, use this example of SQL Server code to update the sales table and change the months column. The goal is to give it a length of 100 characters instead of 20 characters.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Script to increase the length of the NVARCHAR field from 20 to 100 characters\nALTER TABLE [dbo].[Sales]\n  ALTER COLUMN [MonthName] NVARCHAR(100);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Change the length of an SQL column without losing data<\/h2>\n\n\n\n<p>Modifying a table allows you to change the data stored in the table without deleting it. To modify a SQL Server table, <strong><em>the data already stored in the table must be compatible<\/em><\/strong> with the new <a href=\"https:\/\/www.w3resource.com\/sql\/data-type.php\" target=\"_blank\" rel=\"noreferrer noopener\">data type<\/a>. If the new data type is not compatible, then perform these actions in this order.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Store the data in a <a href=\"https:\/\/expert-only.com\/en\/t-sql\/create-temporary-table-sql-server\/\">temporary backup table<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/empty-sql-server-table-truncate\/\">Empty the source table<\/a> with a truncate command<\/li>\n\n\n\n<li>Modify the table with the ALTER script above<\/li>\n\n\n\n<li>Insert the data back into the table with an <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-insert-into-from-a-select\/\">INSERT INTO command<\/a><\/li>\n\n\n\n<li>Check the data in the column and in the table, e.g. count the number of rows<\/li>\n\n\n\n<li>Finally <a href=\"https:\/\/expert-only.com\/en\/t-sql\/drop-a-table-with-a-sql-server-script\/\">delete the backup table<\/a> with a DROP TABLE request<\/li>\n<\/ol>\n\n\n\n<p>This method allows you to change a SQL column and keep the data intact. Finally, here is an article that explains how to delete a SQL Server table from a database.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"xdA5BSZEC2\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/drop-a-table-with-a-sql-server-script\/\">How to drop a SQL Server table with a script?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to drop a SQL Server table with a script?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/drop-a-table-with-a-sql-server-script\/embed\/#?secret=pApbRX02sY#?secret=xdA5BSZEC2\" data-secret=\"xdA5BSZEC2\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Finally, here is another tutorial to <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-alter-table\/\">learn the syntax of SQL scripts to modify a table<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to modify a column in a SQL Server table with a script? Tutorial to modify a SQL Server column with a T-SQL ALTER TABLE and ALTER COLUMN command. This SQL code allows you to modify the structure of <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/modify-a-sql-server-column-with-a-script\/\" title=\"Modify a SQL Server column with a script\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10709,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-18735","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/18735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=18735"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/18735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10709"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=18735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=18735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=18735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}