SQL > SQL String Functions > CONVERT Function

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

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])

Next: SQL CONCATENATE

This page was last updated on October 12, 2024.




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