时间,是数据库中一个充满魔法的复杂表现形式。在这篇博客中,我们将探讨在数据库中时间戳(timestamp)、日期(date)、日期时间(datetime)和字符串之间的转换技巧,覆盖主流数据库如MySQL、Oracle、SQL Server、PostgreSQL以及Spark SQL。
SELECT FROM_UNIXTIME(timestamp_column) AS converted_date FROM your_table;
SELECT UNIX_TIMESTAMP(date_column) AS converted_timestamp FROM your_table;
SELECT TO_DATE(timestamp_column, 'YYYY-MM-DD HH24:MI:SS') AS converted_date FROM your_table;
SELECT TO_CHAR(date_column, 'YYYY-MM-DD HH24:MI:SS') AS converted_timestamp FROM your_table;
SELECT CONVERT(DATE, DATEADD(SECOND, timestamp_column, '19700101')) AS converted_date FROM your_table;
SELECT DATEDIFF(SECOND, '19700101', date_column) AS converted_timestamp FROM your_table;
SELECT TO_TIMESTAMP(timestamp_column) AS converted_date FROM your_table;
SELECT EXTRACT(EPOCH FROM date_column) AS converted_timestamp FROM your_table;
SELECT FROM_UNIXTIME(timestamp_column) AS converted_date FROM your_table;
SELECT UNIX_TIMESTAMP(date_column) AS converted_timestamp FROM your_table;
SELECT DATE_FORMAT(datetime_column, '%Y-%m-%d %H:%i:%s') AS converted_string FROM your_table;
SELECT STR_TO_DATE('2023-11-23 12:30:45', '%Y-%m-%d %H:%i:%s') AS converted_datetime FROM your_table;
SELECT TO_CHAR(datetime_column, 'YYYY-MM-DD HH24:MI:SS') AS converted_string FROM your_table;
SELECT TO_DATE('2023-11-23 12:30:45', 'YYYY-MM-DD HH24:MI:SS') AS converted_datetime FROM dual;
SELECT CONVERT(VARCHAR, datetime_column, 120) AS converted_string FROM your_table;
SELECT CONVERT(DATETIME, '2023-11-23 12:30:45', 120) AS converted_datetime FROM your_table;
SELECT TO_CHAR(datetime_column, 'YYYY-MM-DD HH24:MI:SS') AS converted_string FROM your_table;
SELECT TO_TIMESTAMP('2023-11-23 12:30:45', 'YYYY-MM-DD HH24:MI:SS') AS converted_datetime FROM your_table;
SELECT DATE_FORMAT(datetime_column, 'yyyy-MM-dd HH:mm:ss') AS converted_string FROM your_table;
SELECT TO_TIMESTAMP('2023-11-23 12:30:45', 'yyyy-MM-dd HH:mm:ss') AS converted_datetime FROM your_table;
SELECT DATE_FORMAT(date_column, '%Y-%m-%d') AS converted_string FROM your_table;
SELECT STR_TO_DATE('2023-11-23', '%Y-%m-%d') AS converted_date FROM your_table;
SELECT TO_CHAR(date_column, 'YYYY-MM-DD') AS converted_string FROM your_table;
SELECT TO_DATE('2023-11-23', 'YYYY-MM-DD') AS converted_date FROM dual;
SELECT CONVERT(VARCHAR, date_column, 23) AS converted_string FROM your_table;
SELECT CONVERT(DATE, '2023-11-23') AS converted_date FROM your_table;
SELECT TO_CHAR(date_column, 'YYYY-MM-DD') AS converted_string FROM your_table;
SELECT TO_DATE('2023-11-23', 'YYYY-MM-DD') AS converted_date FROM your_table;
SELECT DATE_FORMAT(date_column, 'yyyy-MM-dd') AS converted_string FROM your_table;
SELECT TO_DATE('2023-11-23', 'yyyy-MM-dd') AS converted_date FROM your_table;
SELECT UNIX_TIMESTAMP('2023-11-23 12:30:45') AS converted_timestamp FROM your_table;
SELECT FROM_UNIXTIME(1606119045) AS converted_string FROM your_table;
SELECT TO_TIMESTAMP('2023-11-23 12:30:45', 'YYYY-MM-DD HH24:MI:SS') AS converted_timestamp FROM dual;
SELECT TO_CHAR('2023-11-23 12:30:45', 'YYYY-MM-DD HH24:MI:SS') AS converted_string FROM dual;
SELECT DATEDIFF(SECOND, '19700101', '2023-11-23 12:30:45') AS converted_timestamp FROM your_table;
SELECT CONVERT(VARCHAR, DATEADD(SECOND, 1606119045, '19700101'), 120) AS converted_string FROM your_table;
SELECT EXTRACT(EPOCH FROM TO_TIMESTAMP('2023-11-23 12:30:45', 'YYYY-MM-DD HH24:MI:SS')) AS converted_timestamp FROM your_table;
SELECT TO_CHAR(TO_TIMESTAMP(1606119045)::TIMESTAMPTZ, 'YYYY-MM-DD HH24:MI:SS') AS converted_string FROM your_table;
SELECT UNIX_TIMESTAMP('2023-11-23 12:30:45') AS converted_timestamp FROM your_table;
SELECT FROM_UNIXTIME(1606119045) AS converted_string FROM your_table;
在上述的介绍中,你可以看到在不同数据库中如何进行时间数据的转换。这些可以帮助你更灵活地处理数据库中的时间信息,确保你的数据分析和查询得以顺利进行。学海无涯苦作舟,只争朝夕不负时!