<<<<<<< HEAD to_char | H2 | SQLDoc
Skip to main content

to_char

1. TO_CHAR Function

======= to_char | H2 Function | SQLDoc

to_char

1. TO_CHAR Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The TO_CHAR function in H2 database is used to convert a value of any supported data type into a string.

2. Syntax

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

TO_CHAR(value, format)

Arguments

  • value: The value that needs to be converted to a string. It can be of any supported data type.
  • format: The optional format string that specifies the desired format for the output string. It follows the pattern syntax used in Java's SimpleDateFormat class.

Return

  • The TO_CHAR function returns the input value as a string according to the specified format.

3. Notes

  • The TO_CHAR function can be used with various data types such as numeric, date, and timestamp to convert them into a string representation.
  • If the format argument is not provided, the TO_CHAR function will use a default format based on the data type being converted.
  • The format string can include various pattern symbols such as 'yyyy' for year, 'MM' for month, 'dd' for day, 'HH' for hour, 'mm' for minute, 'ss' for second, etc. Refer to the Java SimpleDateFormat documentation for more details on the available patterns.
  • If the input value is NULL, the TO_CHAR function will return NULL as well.
  • Make sure to use the correct syntax and format pattern while using the TO_CHAR function to avoid errors.

4. Examples

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

Example 1 - Converting an integer to a string:

SELECT TO_CHAR(123) AS converted_value;

Output:

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

Example 2 - Converting a date to a string with a specific format:

SELECT TO_CHAR(CURRENT_DATE(), 'yyyy-MM-dd') AS formatted_date;

Output:

formatted_date
--------------
2022-01-01

Example 3 - Converting a timestamp to a string with a custom format:

SELECT TO_CHAR(CURRENT_TIMESTAMP(), 'yyyy-MM-dd HH:mm:ss') AS formatted_timestamp;

Output:

formatted_timestamp
-------------------
2022-01-01 12:34:56
  • CAST - Convert between data types
  • convert - Convert a value to another data type