Conversion function
1)CAST()
2)CONVERT()
3)TO_CHAR()
4)T0_DATE()
5)TO_NUMBER()
1)CAST()
*The CAST() function in SQL is used to convert a value from one data type to
another.
*SYNTAX:CAST(expression AS target_data_type)
*expression: The value or column you want to convert.
*target_data_type: The data type you want to convert to (e.g., INT, VARCHAR, DATE, DECIMAL,
etc.).
Examples
1. String to Integer
SELECT CAST('123' AS INT) AS ConvertedInt;
Converts the string '123' to an integer.
2. Integer to String
SELECT CAST(456 AS VARCHAR(10)) AS ConvertedString;
Converts the integer 456 into a string with a max length of 10 characters.
3. String to Date
SELECT CAST('2025-08-24' AS DATE) AS ConvertedDate;
Converts the string '2025-08-24' to a DATE data type.
2)CONVERT()
The CONVERT() function is used to convert data from one type to another, similar
to CAST(), but with more control over formatting, especially for dates and
numbers.
SYNTAX:CONVERT(target_data_type, expression [, style])
*target_data_type – The data type to convert to (e.g., INT, VARCHAR, DATE,
etc.).
*expression – The value to be converted.
*style – (Optional) An integer code that defines how to format dates and times.
1. Convert string to integer
SELECT CONVERT(INT, '123') AS ConvertedInt;
✔️ Converts the string '123' to integer.
2. Convert date to string (with style)
SELECT CONVERT(VARCHAR, GETDATE(), 103) AS UK_Date_Format;
✔️ Converts the current date (GETDATE()) to a string in DD/MM/YYYY format using style code 103.
3. Convert decimal to string
SELECT CONVERT(VARCHAR, 12345.67) AS NumberAsText;
✔️ Converts a numeric value to a string.
3)TO_CHAR()
The TO_CHAR() function is used to convert numbers or dates into formatted strings. It's
especially powerful for customizing date/time and numeric output.
✔️ Syntax
TO_CHAR(expression, format)
expression: A number or date value you want to convert.
format: A format model that specifies how the output should appear.
. 1)Date to String
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS FormattedDate FROM dual;
✔️ Outputs: '2025-08-24'
SELECT TO_CHAR(SYSDATE, 'Month DD, YYYY') AS FullDate FROM dual;
✔️ Outputs: 'August 24, 2025'
2. Number to String
SELECT TO_CHAR(12345.67, '9,999.99') AS FormattedNumber FROM dual;
✔️ Outputs: '12,345.67'
3)TO_DATE()
The TO_DATE() function is used to convert a string into a DATE data type, using a specified
format model to interpret the string correctly.
🔹 Syntax
TO_DATE(string, format)
string: The date in string format (e.g., '24-08-2025')
format: A format model that tells SQL how to interpret the string (e.g., 'DD-MM-YYYY')
🔹 Example 1: Basic Usage
SELECT TO_DATE('24-08-2025', 'DD-MM-YYYY') AS ConvertedDate FROM dual;
✔️ Converts the string '24-08-2025' into a DATE type value.
5)TO_NUMBER()
The TO_NUMBER() function is used to convert a string (VARCHAR) to a numeric value
(NUMBER or DECIMAL), based on a specified format.
✔️ Syntax
TO_NUMBER(string, format_model)
string: The string you want to convert to a number.
format_model (optional): Specifies the number format (e.g., commas, decimal places).
🔹 Basic Example
SELECT TO_NUMBER('12345.67') AS ConvertedNumber FROM dual;
✔️ Converts the string '12345.67' to the numeric value 12345.67.