AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL CAST Function |
|
SQL > SQL String Functions >
CAST Function
The CAST function in SQL converts data from one data type to another. For example, we can use the CAST function to convert numeric data into character string data. SQL CAST(expression AS data_type) is the ANSI-standard way to convert a value from one type to another — commonly used to convert numbers to strings, floats to integers, or dates to text.
SyntaxThe syntax of the CAST function is as follows: where [data type] is a valid data type in the RDBMS you are working with. ExamplesWe use the following table for our examples. Table Student_Score
This table contains the following rows: Table Student_Score
Example 1Result:
In Example 1, we use the CAST function to convert the Score column from type FLOAT to INTEGER. When we do this, different RDMBS have different rules on how to handle the numbers after the decimal point. In the above example, the numbers after the decimal point are always truncated. Example 2Result:
In Example 2, we use the CAST function to convert the SCORE column from type FLOAT to CHAR(3). When we do this, we only take the first three characters. So, if there are more than three characters, everything after the first three characters is discarded. Frequently Asked Questions
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.