CAST

An introduction to the expression CAST.

CAST is an expression that is used to change the data type of a value. The syntax for a CAST expression is:

`CAST` (string `AS` format).

Possible formats are INTEGER, DECIMAL, DOUBLE, BOOLEAN, DATETIME, DATE, TIME, GYEAR, GYEARMONTH, and DATETIMESTAMP.

In our dataset the revenue for each account company is stored as a decimal value. If we wanted to see the value as an integer we could run the query:

SELECT account,
       CAST(revenue AS INTEGER)
  FROM accounts

The results would be:

account revenue
Acme Corporation 1,100
Betasoloin 251
Codehow 2,715
Condax 5
Conecom 1,521
Dalttechnology 99
Dambase 2,174

Exercises (Continued from previous section)

There are two ways to do these exercises. The first is to use the “Try query” links to test your queries without saving them. The second is to create a data.world project and save your queries to it. If you are reading this documentation and completing the exercises as a tutorial, you will need to create your own project to save your work. Details and instructions are in the SQL tutorial which has instructions for setting up your project and links to all the current exercises.

Exercise 23

Write a query using the patient, description, and value columns from the observations table to return all the body height measurements for the patients in decimal form.

Try query

Hint

SELECT the columns patient, description, and CAST the value column AS a DECIMAL FROM the observations table WHERE the description column = “Body Height”

See solution