日期转换为字符串
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
将字符串转换为日期
SELECT TO_DATE('2024-12-16', 'YYYY-MM-DD') FROM DUAL;
将日期转换为TIMESTAMP类型;
SELECT CAST(SYSDATE AS TIMESTAMP) FROM DUAL;
将TIMESTAMP类型转换为日期
SELECT CAST(CAST(SYSDATE AS TIMESTAMP) AS DATE) FROM DUAL;
获取当前时间的10位时间戳
SELECT (TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS') -
TO_DATE('1970-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS')) * 86400 AS TIMESTAMP
FROM DUAL;
时间戳10位转TIMESTAMP类型;
SELECT TO_TIMESTAMP(TO_CHAR(1734334571 / (60 * 60 * 24) +
TO_DATE('1970-01-01 08:00:00',
'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS') AS YF_ABS_CARD_DTTM
FROM DUAL
上月第一天
SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE), -2)) + 1,
'yyyy-mm-dd')
FROM DUAL;
上月最后一天
SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE), -1)), 'yyyy-mm-dd')
FROM DUAL;
本月第一天
SELECT TO_CHAR(TRUNC(SYSDATE, 'mm'), 'yyyy-mm-dd') FROM DUAL;
本月最后一天
SELECT TO_CHAR(LAST_DAY(TRUNC(SYSDATE)), 'yyyy-mm-dd') FROM DUAL;
下月第一天
SELECT TO_CHAR(LAST_DAY(SYSDATE) + 1, 'yyyy-mm-dd') FROM DUAL;
下月最后一天
SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE), 1)), 'yyyy-mm-dd')
FROM DUAL;
今年第一天
SELECT TRUNC(SYSDATE, 'yyyy') FROM DUAL;
今年最后一天
SELECT LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE, 'y'), 11)) FROM DUAL
目前工作中用到比较多的实例,有些简单的例子就不一一列举,如果大家有好的意见,请多多留言,十分感谢!
标签:SYSDATE,YYYY,DD,常见,CHAR,日期,DUAL,Oracle,SELECT From: https://blog.csdn.net/qq_45710370/article/details/144509927