<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Changing scalar column data type</title>
    <link>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/</link>
    <description>Recent content in Changing scalar column data type on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Admin: Changing column width</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/changing-column-width/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/changing-column-width/</guid>
      <description>
        
        
        &lt;p&gt;You can expand columns within the same class of data type. Doing so is useful for storing larger items in a column. The database validates the data before it performs the conversion.&lt;/p&gt;
&lt;p&gt;In general, you can also reduce column widths within the data type class. This is useful to reclaim storage if the original declaration was longer than you need, particularly with strings. You can reduce column width only if the following conditions are true:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Existing column data is no greater than the new width.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;All nodes in the database cluster are up.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Otherwise, the database returns an error and the conversion fails. For example, if you try to convert a column from &lt;code&gt;varchar(25)&lt;/code&gt; to &lt;code&gt;varchar(10)&lt;/code&gt;, the database allows the conversion as long as all column data is no more than 10 characters.&lt;/p&gt;
&lt;p&gt;In the following example, columns &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; are initially defined as VARCHAR data types, and loaded with values &lt;code&gt;12345&lt;/code&gt; and &lt;code&gt;654321&lt;/code&gt;, respectively. The attempt to reduce column &lt;code&gt;z&lt;/code&gt;&#39;s width to 5 fails because it contains six-character data. The attempt to reduce column &lt;code&gt;y&lt;/code&gt;&#39;s width to 5 succeeds because its content conforms with the new width:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE t (x int, y VARCHAR, z VARCHAR);
CREATE TABLE
=&amp;gt; CREATE PROJECTION t_p1 AS SELECT * FROM t SEGMENTED BY hash(x) ALL NODES;
CREATE PROJECTION
=&amp;gt; INSERT INTO t values(1,&amp;#39;12345&amp;#39;,&amp;#39;654321&amp;#39;);
 OUTPUT
--------
      1
(1 row)

=&amp;gt; SELECT * FROM t;
 x |   y   |   z
---+-------+--------
 1 | 12345 | 654321
(1 row)

=&amp;gt; ALTER TABLE t ALTER COLUMN z SET DATA TYPE char(5);
ROLLBACK 2378:  Cannot convert column &amp;#34;z&amp;#34; to type &amp;#34;char(5)&amp;#34;
HINT:  Verify that the data in the column conforms to the new type
=&amp;gt; ALTER TABLE t ALTER COLUMN y SET DATA TYPE char(5);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a name=&#34;Changing&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;changing-collection-columns&#34;&gt;Changing collection columns&lt;/h2&gt;
&lt;p&gt;If a column is a collection data type, you can use ALTER TABLE to change either its bounds or its maximum binary size. These properties are set at table creation time and can then be altered.&lt;/p&gt;
&lt;p&gt;You can make a collection bounded, setting its maximum number of elements, as in the following example.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE test.t1 ALTER COLUMN arr SET DATA TYPE array[int,10];
ALTER TABLE

=&amp;gt; \d test.t1
                                     List of Fields by Tables
 Schema | Table | Column |      Type       | Size | Default | Not Null | Primary Key | Foreign Key
--------+-------+--------+-----------------+------+---------+----------+-------------+-------------
  test  |  t1   | arr    | array[int8, 10] |   80 |         | f        | f           |
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Alternatively, you can set the binary size for the entire collection instead of setting bounds. Binary size is set either explicitly or from the DefaultArrayBinarySize configuration parameter. The following example creates an array column from the default, changes the default, and then uses ALTER TABLE to change it to the new default.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT get_config_parameter(&amp;#39;DefaultArrayBinarySize&amp;#39;);
 get_config_parameter
----------------------
 100
(1 row)

=&amp;gt; CREATE TABLE test.t1 (arr array[int]);
CREATE TABLE

=&amp;gt; \d test.t1
                                     List of Fields by Tables
 Schema | Table | Column |      Type       | Size | Default | Not Null | Primary Key | Foreign Key
--------+-------+--------+-----------------+------+---------+----------+-------------+-------------
  test  |  t1   | arr    | array[int8](96) |   96 |         | f        | f           |
(1 row)

=&amp;gt; ALTER DATABASE DEFAULT SET DefaultArrayBinarySize=200;
ALTER DATABASE

=&amp;gt; ALTER TABLE test.t1 ALTER COLUMN arr SET DATA TYPE array[int];
ALTER TABLE

=&amp;gt; \d test.t1
                                     List of Fields by Tables
 Schema | Table | Column |      Type       | Size | Default | Not Null | Primary Key | Foreign Key
--------+-------+--------+-----------------+------+---------+----------+-------------+-------------
  test  |  t1   | arr    | array[int8](200)|  200 |         | f        | f           |
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Alternatively, you can set the binary size explicitly instead of using the default value.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE test.t1 ALTER COLUMN arr SET DATA TYPE array[int](300);
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;purging-historical-data&#34;&gt;Purging historical data&lt;/h2&gt;
&lt;p&gt;You cannot reduce a column&#39;s width if the database retains any historical data that exceeds the new width. To reduce the column width, first remove that data from the table:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Advance the AHM to an epoch more recent than the historical data that needs to be removed from the table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Purge the table of all historical data that precedes the AHM with the function 
&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/management-functions/table-functions/purge-table/#&#34;&gt;PURGE_TABLE&lt;/a&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, given the previous example, you can update the data in column &lt;code&gt;t.z&lt;/code&gt; as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; UPDATE t SET z = &amp;#39;54321&amp;#39;;
 OUTPUT
--------
      1
(1 row)

=&amp;gt; SELECT * FROM t;
 x |   y   |   z
---+-------+-------
 1 | 12345 | 54321
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Although no data in column z now exceeds 5 characters, the database retains the history of its earlier data, so attempts to reduce the column width to 5 return an error:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE t ALTER COLUMN z SET DATA TYPE char(5);
ROLLBACK 2378:  Cannot convert column &amp;#34;z&amp;#34; to type &amp;#34;char(5)&amp;#34;
HINT:  Verify that the data in the column conforms to the new type
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can reduce the column width by purging the table&#39;s historical data as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT MAKE_AHM_NOW();
         MAKE_AHM_NOW
-------------------------------
 AHM set (New AHM Epoch: 6350)
(1 row)

=&amp;gt; SELECT PURGE_TABLE(&amp;#39;t&amp;#39;);
                                                     PURGE_TABLE
----------------------------------------------------------------------------------------------------------------------
 Task: purge operation
(Table: public.t) (Projection: public.t_p1_b0)
(Table: public.t) (Projection: public.t_p1_b1)

(1 row)

=&amp;gt; ALTER TABLE t ALTER COLUMN z SET DATA TYPE char(5);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Admin: Working with column data conversions</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/working-with-column-data-conversions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/working-with-column-data-conversions/</guid>
      <description>
        
        
        &lt;p&gt;The database conforms to the SQL standard by disallowing certain data conversions for table columns. However, you sometimes need to work around this restriction when you convert data from a non-SQL database. The following examples describe one such workaround, using the following table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE sales(id INT, price VARCHAR) UNSEGMENTED ALL NODES;
CREATE TABLE
=&amp;gt; INSERT INTO sales VALUES (1, &amp;#39;$50.00&amp;#39;);
 OUTPUT
--------
      1
(1 row)

=&amp;gt; INSERT INTO sales VALUES (2, &amp;#39;$100.00&amp;#39;);
 OUTPUT
--------
      1
(1 row)

=&amp;gt; COMMIT;
COMMIT
=&amp;gt; SELECT * FROM SALES;
 id |  price
----+---------
  1 | $50.00
  2 | $100.00
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To convert the &lt;code&gt;price&lt;/code&gt; column&#39;s existing data type from VARCHAR to NUMERIC, complete these steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#Add&#34;&gt;Add a new column for temporary use&lt;/a&gt;. Assign the column a NUMERIC data type, and derive its default value from the existing price column.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#Advance&#34;&gt;Drop the original price column&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#Rename&#34;&gt;Rename the new column to the original column&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;Add&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-a-new-column-for-temporary-use&#34;&gt;Add a new column for temporary use&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Add a column &lt;code&gt;temp_price&lt;/code&gt; to table &lt;code&gt;sales&lt;/code&gt;. You can use the new column temporarily, setting its data type to what you want (NUMERIC), and deriving its default value from the &lt;code&gt;price&lt;/code&gt; column. Cast the default value for the new column to a NUMERIC data type and query the table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE sales ADD COLUMN temp_price NUMERIC(10,2) DEFAULT
SUBSTR(sales.price, 2)::NUMERIC;
ALTER TABLE

=&amp;gt; SELECT * FROM SALES;
 id |  price  | temp_price
----+---------+------------
  1 | $50.00  |      50.00
  2 | $100.00 |     100.00
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use ALTER TABLE to drop the default expression from the new column &lt;code&gt;temp_price&lt;/code&gt;. The database retains the values stored in this column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE sales ALTER COLUMN temp_price DROP DEFAULT;
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;Advance&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;drop-the-original-price-column&#34;&gt;Drop the original price column&lt;/h2&gt;
&lt;p&gt;Drop the extraneous &lt;code&gt;price&lt;/code&gt; column. Before doing so, you must first advance the &lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/ancient-history-mark-ahm/&#34; title=&#34;Also known as AHM, the ancient history mark is the oldest epoch whose data is accessible to historical queries.&#34;&gt;AHM&lt;/a&gt; to purge historical data that would otherwise prevent the drop operation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Advance the AHM:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT MAKE_AHM_NOW();
         MAKE_AHM_NOW
-------------------------------
 AHM set (New AHM Epoch: 6354)
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Drop the original price column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE sales DROP COLUMN price CASCADE;
ALTER COLUMN
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;Rename&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;rename-the-new-column-to-the-original-column&#34;&gt;Rename the new column to the original column&lt;/h2&gt;
&lt;p&gt;You can now rename the &lt;code&gt;temp_price&lt;/code&gt; column to &lt;code&gt;price&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;ALTER TABLE&lt;/code&gt; to rename the column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE sales RENAME COLUMN temp_price to price;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Query the sales table again:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM sales;
 id | price
----+--------
  1 |  50.00
  2 | 100.00
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
  </channel>
</rss>
