目录
SQL常用日期查询语句及显示格式设置
1.查询当前年、月、周相关时间
2.查询当前第几季度
3.查询当前在一年内是第几周
SqlServer中的日期与时间函数
SQL查询前10条的方法为:
SQL常用日期查询语句及显示格式设置
1.查询当前年、月、周相关时间
1.1.查询当前年份
SELECT TO_CHAR(SYSDATE,'YYYY') AS YEAR FROM DUAL--查询当前年份SELECT TO_CHAR(SYSDATE,'YYY') AS YEAR FROM DUAL--查询当前年份后两位SELECT TO_CHAR(SYSDATE,'YY') AS YEAR FROM DUAL--查询当前年份最后两位SELECT TO_CHAR(SYSDATE,'Y') AS YEAR FROM DUAL--查询当前年份最后一位
1.2.查询当前年份第几天
SELECT TO_CHAR(SYSDATE,'DDD') AS DAYS FROM DUAL
1.3.查询当前月份第几天
SELECT TO_CHAR(SYSDATE,'DD') AS DAYS FROM DUAL
1.4.查询当前周的星期几
SELECT TO_CHAR(SYSDATE,'dy') AS WEEK FROM DUALSELECT TO_CHAR(SYSDATE,'day') AS WEEK FROM DUAL
1.5.查询当前年的第几天:
SELECT TO_CHAR(SYSDATE,'DDD') AS DAYS FROM DUAL
2.查询当前第几季度
SELECT TO_CHAR(SYSDATE,'Q') AS JD FROM DUAL
3.查询当前在一年内是第几周
SELECT TO_CHAR(SYSDATE,'IW') AS WEEKNUM FROM DUALSELECT TO_CHAR(SYSDATE,'WW') AS WEEKNUM FROM DUALSELECT TO_CHAR(SYSDATE,'W') AS WEEKNUM FROM DUAL
4.查询当前某年某月有多少天
SELECT TO_CHAR(last_day(to_date('/06/03','YYYY/MM/DD')),'DD') AS MONTH_DAYS_NUMBER FROM DUAL
5.查询当前的日期格式
SELECT TO_CHAR(SYSDATE,'YYYY/MM/DD') AS MONTH FROM DUAL--获取当前日期的'YYYY/MM/DD'格式:/01/02SELECT TO_CHAR(SYSDATE,'YYYYMMDD') AS MONTH FROM DUAL--获取当前日期的'YYYYMMDD'格式:0102SELECT TO_CHAR(SYSDATE,'YYYYMMDD HH:MI:SS') AS now FROM DUAL--获取当前时间,精确到秒的12小时制格式:0102 03:18:25SELECT TO_CHAR(SYSDATE,'YYYYMMDD HH24:MI:SS') AS now FROM DUAL--获取当前时间,精确到秒24小时制的格式:0102 15:18:13SELECT TO_CHAR(SYSDATE,'YYYY/MM/DD HH24:MI:SS:PM:DY') AS now FROM DUAL--获取当前时间的格式,显示上、下午和星期几:/01/02 15:17:59:下午:星期四
6.查询距离当前时间一段时间间隔的时间,当前时间之前用“-”当前时间之后用“+”
SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7' second,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7秒前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7' minute,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7分钟前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7'hour ,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7小时前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7'day ,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7天前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7'month ,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7月前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - INTERVAL '7'year ,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间的7年前时间SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS NOW,TO_CHAR(SYSDATE - 8*INTERVAL '7'hour ,'YYYY/MM/DD HH24:MI:SS') AS PAST FROM DUAL --查询当前时间参数年、月、日、时、分、秒乘以一个数字倍数之前的时间
SqlServer中的日期与时间函数
参数 interval的设定值如下:
SQL查询前10条的方法为:
select top X * from table_name select top X * from table_name order by colum_name desc--MySQL、Sqlite查询前10条数据的方法:select * from table_name limit 0,10 --ORACLE查询前10条的方法:select * from table_name where rownum<X
如果觉得《SQL常用(查询日期)语句》对你有帮助,请点赞、收藏,并留下你的观点哦!