ALTER TABLE … ALTER COLUMN … SET DATA TYPE converts a column to a new data type when the existing data can be cast.
Data models evolve. Converting a VARCHAR price column to NUMBER improves numeric accuracy, enables math functions, and tightens validation—without recreating the table.
Use ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_type
. Include IF EXISTS
to avoid errors on missing tables or columns.
Run SELECT column_name::new_type
on a sample to confirm casting works. Wrap the change in a transaction so you can ROLLBACK
if unexpected errors appear.
--Verify that all prices are numeric
SELECT price FROM Products WHERE TRY_TO_NUMBER(price) IS NULL;
--Alter the column
ALTER TABLE Products ALTER COLUMN price SET DATA TYPE NUMBER(10,2);
Back up critical tables with CREATE TABLE … CLONE
. Run the change during low-traffic windows. Always add or rebuild indexes and constraints after the type switch.
Views referencing the altered column pick up the new type automatically. Re-run test queries to catch casting assumptions.
No. ALTER COLUMN only changes the data type; column order remains intact.
Snowflake performs the change in-place and online. Large tables may briefly lock DML, so schedule during low activity.
Use BEGIN
… ROLLBACK
or restore from a zero-copy CLONE
.