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;