Table of contents
SQL common date query statement and display format setting
1. Query the relevant time of the current year, month and week
3. Query the current week in a year
Date and time functions in SqlServer
The method of querying the first 10 items in SQL is as follows:
SQL common date query statement and display format setting
1. Query the relevant time of the current year, month and week
1.1. Query the current year
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. Query the day of the current year
SELECT TO_CHAR(SYSDATE,'DDD') AS DAYS FROM DUAL
1.3. Query the day of the current month
SELECT TO_CHAR(SYSDATE,'DD') AS DAYS FROM DUAL
1.4. Query the day of the week in the current week
SELECT TO_CHAR(SYSDATE,'dy') AS WEEK FROM DUAL
SELECT TO_CHAR(SYSDATE,'day') AS WEEK FROM DUAL
1.5. Query the day of the current year:
SELECT TO_CHAR(SYSDATE,'DDD') AS DAYS FROM DUAL
2. Query the current quarter
SELECT TO_CHAR(SYSDATE,'Q') AS JD FROM DUAL
3. Query the current week in a year
SELECT TO_CHAR(SYSDATE,'IW') AS WEEKNUM FROM DUAL
SELECT TO_CHAR(SYSDATE,'WW') AS WEEKNUM FROM DUAL
SELECT TO_CHAR(SYSDATE,'W') AS WEEKNUM FROM DUAL
4. Query the number of days in a certain month in the current year
SELECT TO_CHAR(last_day(to_date('2023/06/03','YYYY/MM/DD')),'DD') AS MONTH_DAYS_NUMBER FROM DUAL
5. Query the current date format
SELECT TO_CHAR(SYSDATE,'YYYY/MM/DD') AS MONTH FROM DUAL--获取当前日期的'YYYY/MM/DD'格式:2020/01/02
SELECT TO_CHAR(SYSDATE,'YYYYMMDD') AS MONTH FROM DUAL--获取当前日期的'YYYYMMDD'格式:20200102
SELECT TO_CHAR(SYSDATE,'YYYYMMDD HH:MI:SS') AS now FROM DUAL--获取当前时间,精确到秒的12小时制格式:20200102 03:18:25
SELECT TO_CHAR(SYSDATE,'YYYYMMDD HH24:MI:SS') AS now FROM DUAL--获取当前时间,精确到秒24小时制的格式:20200102 15:18:13
SELECT TO_CHAR(SYSDATE,'YYYY/MM/DD HH24:MI:SS:PM:DY') AS now FROM DUAL--获取当前时间的格式,显示上、下午和星期几:2020/01/02 15:17:59:下午:星期四
6. Query the time interval from the current time, use "-" before the current time and use "+" after the current time
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 --查询当前时间参数年、月、日、时、分、秒乘以一个数字倍数之前的时间
Date and time functions in SqlServer
function | parameter/function |
getDate() | Returns the current date and time of the system |
DateDiff (interval,date1,date2) | In the way specified by interval, return the difference between date2 and date1 date2-date1 |
DateAdd (interval,number,date) | In the way specified by interval, add the date after number |
DatePart (interval,date) | Returns the integer value corresponding to the part specified by interval in date |
DateName (interval,date) | Returns the string name corresponding to the part specified by interval in date |
The setting value of the parameter interval is as follows:
value |
Abbreviation (Sql Server) | Access and ASP | illustrate |
Year | Yy | yyyy | Year 1753 ~ 9999 |
Quarter | q | Season 1 ~ 4 | |
Month | Mm | m | month 1 ~ 12 |
Day of year | Dy | y | The number of days in a year, the day of the year 1-366 |
Day | Dd | d | Sun, 1-31 |
Weekday | Dw | w | The number of days of the week, the number of days of the week 1-7 |
Week | Wk | ww | Week, the week of the year 0 ~ 51 |
Hour | Hh | h | hour 0 ~ 23 |
Minute | Mi | n | minutes 0 ~ 59 |
Second | Ss | s | seconds 0 ~ 59 |
Millisecond | Ms | - | milliseconds 0 ~ 999 |
The method of querying the first 10 items in SQL is as follows:
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