SQL > SQL String Functions > CONVERT Function

The CONVERT function in MySQL and SQL Server converts data from one data type to another.

The SQL CONVERT function lets you change a value's data type — for example, converting a float to an integer — with slightly different syntax depending on the database (MySQL vs. SQL Server vs. Oracle).

Syntax

The syntax of the CONVERT function is as follows:

CONVERT (expression, [data type])

where [data type] is a valid data type in the RDBMS you are working with.

Example

We use the following table for our example.

Table Student_Score

 Column Name  Data Type 
 StudentID  integer 
 First_Name  char(20) 
 Score  float 

This table contains the following rows:

Table Student_Score

 StudentID  First_Name  Score 
1Jenny 85.2 
2Bob 92.5 
3Alice 90 
4James 120.1 

The SQL statement,

SELECT First_Name, CONVERT(Score, Integer) Int_Score FROM Student_Score;

produces the following result set:

First_Name Int_Score
Jenny 85
Bob 92
Alice 90
James 120

CONVERT function in Oracle

In Oracle, the CONVERT function is used differently. It converts a string from one character set to another.

Syntax in Oracle

CONVERT (string, [new character set], [original character set])

Frequently Asked Questions

Q: What does the SQL CONVERT function do?
A: In MySQL and SQL Server, CONVERT converts a value from one data type to another. In Oracle, it converts a string from one character set to another.

Q: How is CONVERT different from CAST?
A: CAST is ANSI SQL standard and uses CAST(expression AS data_type) syntax. CONVERT is database-specific and uses CONVERT(expression, data_type) in MySQL or CONVERT(data_type, expression) in SQL Server.

Q: Can CONVERT truncate decimal values?
A: Yes. Converting a float to an integer with CONVERT will truncate the decimal portion — for example, 85.2 becomes 85.

Q: Is CONVERT available in all SQL databases?
A: CONVERT is available in MySQL, SQL Server, and Oracle, but with different syntax and behavior. PostgreSQL uses CAST or the :: operator for type conversion instead.

Next: SQL CONCATENATE

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact