Aan de slagGa gratis aan de slag

Convert types USING a function

If you don't want to reserve too much space for a certain varchar column, you can truncate the values before converting its type.

For this, you can use the following syntax:

ALTER TABLE table_name
ALTER COLUMN column_name
TYPE varchar(x)
USING SUBSTRING(column_name FROM 1 FOR x)

You should read it like this: Because you want to reserve only x characters for column_name, you have to retain a SUBSTRING of every value, i.e. the first x characters of it, and throw away the rest. This way, the values will fit the varchar(x) requirement.

Deze oefening maakt deel uit van de cursus

Introduction to Relational Databases in SQL

Cursus bekijken

Oefeninstructies

  • Run the sample code as is and take note of the error.
  • Now use SUBSTRING() to reduce firstname to 16 characters so its type can be altered to varchar(16).

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

-- Convert the values in firstname to a max. of 16 characters
ALTER TABLE professors 
ALTER COLUMN firstname 
TYPE varchar(16)
Code bewerken en uitvoeren