SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | Conversion Function

SQL provides various conversion functions to convert data from one type to another. These functions can be essential when you're dealing with data from various sources, or when you need to transform data types for specific operations.

The exact functions available and their behavior can vary between different relational database management systems (RDBMS), but here are some general and commonly used conversion functions:

1. CAST:

The CAST function is a standard SQL function used to convert one data type to another.

Syntax:

CAST(expression AS datatype)

Example:

SELECT CAST(salary AS CHAR(10)) 
FROM employees;

2. CONVERT:

CONVERT is another function used for type conversion, and it's often associated with SQL Server.

Syntax:

CONVERT(data_type(length), expression, style)

The style parameter is optional and used for formatted conversions, such as date formats.

Example:

-- SQL Server specific example for date conversion
SELECT CONVERT(CHAR(10), GETDATE(), 103) AS 'Date'; 

RDBMS-Specific Conversion Functions:

Different databases might have their specific functions for common conversions:

Oracle:

  • TO_DATE: Converts a string to a date.

    SELECT TO_DATE('2021-01-01', 'YYYY-MM-DD') FROM dual;
    
  • TO_NUMBER: Converts a string to a number.

    SELECT TO_NUMBER('1234.56') FROM dual;
    
  • TO_CHAR: Converts a date or number to a string.

    SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') FROM dual;
    

SQL Server:

  • STR: Converts numeric data to string.
    SELECT STR(25.65, 10, 2);
    

PostgreSQL:

  • :: (Type Casting): PostgreSQL allows for type casting using the :: operator.
    SELECT '123'::integer;
    SELECT current_date::varchar;
    

Tips and Considerations:

  1. Be Careful with Conversions: While converting between data types, there's a possibility of data loss, especially when converting from a larger type (like FLOAT) to a smaller one (like INT), or when converting between incompatible types.

  2. Check Destination Type Capacity: Make sure that the destination data type can accommodate the data. For example, converting a long string into a CHAR(5) will truncate the string.

  3. Dates and Time Zones: When dealing with date and time conversions, always be aware of the time zones and date formats to avoid misinterpretations.

  4. RDBMS Differences: Always refer to your RDBMS documentation when working with conversion functions, as there can be nuances in function names, behaviors, and supported data types.

  5. Performance: Frequent or unnecessary conversions might have an impact on query performance. Avoid them when possible, and ensure data is stored in the most appropriate format from the outset.

  1. How to Use CAST and CONVERT Functions in SQL:

    • Using CAST:
      SELECT column1, CAST(column2 AS VARCHAR(50)) AS converted_result
      FROM your_table;
      
    • Using CONVERT:
      SELECT column1, CONVERT(VARCHAR(50), column2) AS converted_result
      FROM your_table;
      
  2. Using TO_DATE Function in SQL:

    • TO_DATE is typically used in databases like Oracle.
      SELECT TO_DATE('2023-01-01', 'YYYY-MM-DD') AS converted_date
      FROM dual;
      
  3. SQL TO_NUMBER Function Usage:

    • TO_NUMBER is often used in databases like Oracle.
      SELECT TO_NUMBER('12345.67', '99999.99') AS converted_number
      FROM dual;
      
  4. TO_CHAR Function in SQL for Date Formatting:

    • TO_CHAR is often used for date formatting.
      SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS formatted_date
      FROM dual;
      
  5. CONVERTING Function in SQL with Examples:

    • CONVERTING is database-specific. Example for SQL Server:
      SELECT CONVERT(VARCHAR, GETDATE(), 120) AS formatted_date;
      
  6. Implicit and Explicit Data Type Conversion in SQL:

    • Implicit conversion happens automatically.
    • Explicit conversion using functions like CAST and CONVERT.
      SELECT CAST(column1 AS INT) + 10 AS result
      FROM your_table;
      
  7. SQL Conversion Functions for String to Date:

    SELECT column1, CAST(column2 AS DATE) AS converted_date
    FROM your_table;
    
  8. Handling NULL Values in SQL Conversion Functions:

    • Using COALESCE to handle NULL values.
      SELECT COALESCE(CAST(column1 AS INT), 0) AS result
      FROM your_table;
      
  9. SQL Conversion Functions for Numeric to String:

    SELECT column1, CAST(column2 AS VARCHAR(50)) AS converted_string
    FROM your_table;
    
  10. Using FORMAT Function in SQL:

    • FORMAT is often used for formatting numbers and dates.
      SELECT column1, FORMAT(column2, 'C') AS formatted_currency
      FROM your_table;
      
  11. SQL Conversion Functions in SELECT Statement:

    • You can use conversion functions directly in the SELECT statement.
      SELECT column1, CAST(column2 AS DECIMAL(10, 2)) AS converted_result
      FROM your_table;