Skip to main content

convert

1. CONVERT Function

The CONVERT function in H2 database is used to convert a value from one data type to another.

2. Syntax

The syntax for the CONVERT function in H2 database is as follows:

CONVERT(value, target_type)

Arguments

  • value: The value that needs to be converted. It can be of any valid data type in H2 database.
  • target_type: The target data type to which the value should be converted. It should be a valid data type supported by H2 database.

Return

  • The CONVERT function returns the value after converting it to the specified target data type.

3. Notes

  • The CONVERT function in H2 database can be used to convert values between different data types, such as converting a string to a number, or a number to a string.
  • If the conversion is not possible due to incompatible data types or other constraints, the CONVERT function will return NULL.
  • It is important to ensure that the value being converted is compatible with the target data type to avoid errors.

4. Examples

Here are a few examples demonstrating the usage of the CONVERT function in H2 database:

Example 1 - Converting a string to an integer:

SELECT CONVERT('123', INT) AS converted_value;

Output:

converted_value
---------------
123

Example 2 - Converting a decimal to a string:

SELECT CONVERT(3.14, VARCHAR) AS converted_value;

Output:

converted_value
---------------
3.14

Example 3 - Converting a timestamp to a date:

SELECT CONVERT(CURRENT_TIMESTAMP(), DATE) AS converted_value;

Output:

converted_value
---------------
2023-10-20
  • CAST - Explicitly cast a value to a specified data type
  • to_char - Format a value as a string