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 NameData Type
StudentIDinteger
First_Namechar(20)
Scorefloat

This table contains the following rows:

Table Student_Score

StudentIDFirst_NameScore
1Jenny85.2
2Bob92.5
3Alice90
4James120.1

The SQL statement,

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

produces the following result set:

First_NameInt_Score
Jenny85
Bob92
Alice90
James120

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




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