Skip to main content

Use type conversion functions in queries

This topic provides examples of how to use type conversion functions in queries.

Type conversion functions

Type conversion functions convert one data type to another, such as between numeric types and string types, or between time types and string types.

Examples

Convert a time string to a time type

In seekdb, a time string can be directly assigned to a DATE type, and seekdb will automatically convert it to a time type. You can also use the CONVERT() or CAST() function to convert data types. Here is an example SQL statement:

SELECT CONVERT('2020-02-02 14:30:45', DATE)    t1
, CONVERT('2020-02-02 14:30:45', TIME) t2
, CONVERT('2020-02-02 14:30:45', DATETIME) t3
, CAST('2020-02-02 14:30:45' AS DATE) t4
, CAST('2020-02-02 14:30:45' AS TIME) t5
, CAST('2020-02-02 14:30:45' AS DATETIME) t6
\G

*************************** 1. row ***************************
t1: 2020-02-02
t2: 14:30:45
t3: 2020-02-02 14:30:45
t4: 2020-02-02
t5: 14:30:45
t6: 2020-02-02 14:30:45
1 row in set
info

You can use the DATE_FORMAT function to convert a time type to a string type.

Convert between numeric and string types

In seekdb, you can use the CONVERT() or CAST() function to convert between numeric and string types.

SELECT CONVERT('3.1415926', DECIMAL)  n1
, CAST('3.1415926' AS DECIMAL) n2
, CONVERT(3.1415926, CHAR(10)) s1
, CAST(3.1414926 AS CHAR(10)) s2;
+----+----+-----------+-----------+
| n1 | n2 | s1 | s2 |
+----+----+-----------+-----------+
| 3 | 3 | 3.1415926 | 3.1414926 |
+----+----+-----------+-----------+
1 row in set