解锁时间数据的魔法
时间,是数据库中一个充满魔法的复杂表现形式。在这篇博客中,我们将探讨在数据库中时间戳(timestamp)、日期(date)、日期时间(datetime)和字符串之间的转换技巧,覆盖主流数据库如MySQL、Oracle、SQL Server、PostgreSQL以及Spark SQL。
1. 时间戳(Timestamp)与日期(Date)之间的转换
1.1 MySQL
时间戳转为日期:
SELECT FROM_UNIXTIME(timestamp_column) AS converted_date FROM your_table;
日期转为时间戳:
SELECT UNIX_TIMESTAMP(date_column) AS converted_timestamp FROM your_table;
1.2 Oracle
时间戳转为日期:
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;
1.3 SQL Server
时间戳转为日期:
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;
1.4 PostgreSQL
时间戳转为日期:
SELECT TO_TIMESTAMP(timestamp_column) AS converted_date FROM your_table;
日期转为时间戳:
SELECT EXTRACT(EPOCH FROM date_column) AS converted_timestamp FROM your_table;
1.5 Spark SQL
时间戳转为日期:
SELECT FROM_UNIXTIME(timestamp_column) AS converted_date FROM your_table;
日期转为时间戳:
SELECT UNIX_TIMESTAMP(date_column) AS converted_timestamp FROM your_table;
2. 日期时间(Datetime)与字符串之间的转换
2.1 MySQL
日期时间转为字符串:
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;
2.2 Oracle
日期时间转为字符串:
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;
2.3 SQL Server
日期时间转为字符串:
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;
2.4 PostgreSQL
日期时间转为字符串:
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;
2.5 Spark SQL
日期时间转为字符串:
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;
3. 日期(Date)与字符串之间的转换
3.1 MySQL
日期转为字符串:
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;
3.2 Oracle
日期转为字符串:
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;
3.3 SQL Server
日期转为字符串:
SELECT CONVERT(VARCHAR, date_column, 23) AS converted_string FROM your_table;
字符串转为日期:
SELECT CONVERT(DATE, '2023-11-23') AS converted_date FROM your_table;
3.4 PostgreSQL
日期转为字符串:
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;
3.5 Spark SQL
日期转为字符串:
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;
4. 字符串与时间戳之间的转换
4.1 MySQL
字符串转为时间戳:
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;
4.2 Oracle
字符串转为时间戳:
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;
4.3 SQL Server
字符串转为时间戳:
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;
4.4 PostgreSQL
字符串转为时间戳:
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;
4.5 Spark SQL
字符串转为时间戳:
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;
写在最后
在上述的介绍中,你可以看到在不同数据库中如何进行时间数据的转换。这些可以帮助你更灵活地处理数据库中的时间信息,确保你的数据分析和查询得以顺利进行。学海无涯苦作舟,只争朝夕不负时!