sql statement to query data specified month

Requirements: query the emp table in February 1981 recruits

 

emp table

image.png

Commonly used in two ways:

1.YEAR query years, MONTH month inquiry

SELECT
    *
FROM
    emp
WHERE 
    YEAR (HIREDATE) = ' 1981 '  AND  MONTH (HIREDATE) = ' 2 '

 

2.date_format (use more)

SELECT 
    *
FROM
    emp
WHERE
    DATE_FORMAT(hiredate,'%Y-%m') = '1981-02'

The second way, to note that the date format must be correct

E.g:

DATE_FORMAT(hiredate,'%Y-%m') = '1981-2'

It is unable to locate data because the% m corresponding month format is: 01,02,03 ... 12.

If replaced

DATE_FORMAT(hiredate,'%Y-%c') = '1981-2'

You will be able to query the data.

 

** MySQL date format

% Y represents the four-digit year% y represents two-year

% M is the month format (01,02,03 ... 12)% c is the month format (1,2,3 ... 12)

% D means day

% H Representative Representative 24 hour 12 hour% h

% I represents the minutes (00, -01,02 ... 59)

% S% s for seconds or (00, 01 ... 59)

 

Guess you like

Origin www.cnblogs.com/jr-xiaojian/p/12327859.html