loading..
Русский    English
15:59

DATEPART function page 2

MySQL

In the MySQL dialect, each date component can be extracted from a date value with the correspondent function. Suppose we must query the Airport database to get minutes of department time of the flights that departs between 1 and 2 p.m.

Solution 1

  1. SELECT time_out, MINUTE(time_out) 
  2. FROM trip
  3. WHERE HOUR(time_out) = 12;
time_out    min
1900-01-01 12:35:00   35
1900-01-01 12:00:00   0

Time components can also be got with the EXTRACT function. In this case the solution can be written in the following form:

Solution 2

  1. SELECT time_out, EXTRACT(MINUTE FROM time_out) AS MIN
  2. FROM trip
  3. WHERE EXTRACT(HOUR FROM time_out) = 12;

The EXTRACT function can also return composite components of date and/or time, for example, year and month. Let's solve the following exercise.

Query the Paintings database to count the number of paintings in months of each year.


Solution 3

  1. SELECT YEAR(b_datetime) AS y, MONTH(b_datetime) AS m,
  2. EXTRACT(YEAR_MONTH FROM b_datetime) AS ym, COUNT(*) AS qty
  3. FROM utb
  4. GROUP BY EXTRACT(YEAR_MONTH FROM b_datetime);

m   ym     qty
2000 1 200001 1
2001 1 200101 1
2002 1 200201 1
2002 6 200206 1
2003 1 200301 69
2003 2 200302 7
2003 3 200303 2
2003 4 200304 2
2003 5 200305 2
2003 6 200306 2

The full list of possible components can be found, for example, here.

Notes:

It is appropriate to mention here rather free style of MySQL grouping. Columns of a SELECT clause are allowed not to be present in the GROUP BY column list. It is obvious that an implicit aggregate function is supposed to be there, otherwise it would be impossible to interpret the statement unambiguously. I suppose that MIN or MAX functions are used implicitly but I'm not going to make it out here, because this feature contradicts SQL standard.

PostgreSQL

As far as I know, there are no functions like YEAR, MONTH, etc. in PostgreSQL. Still the EXTRACT function exists, and the second solution variant that we've written for MySQL is valid for this DBMS too:

  1. SELECT time_out, EXTRACT(MINUTE FROM time_out) AS MIN
  2. FROM trip
  3. WHERE EXTRACT(HOUR FROM time_out) = 12;

There is yet a function analogous to the DATEPART of MS SQL. A syntactical difference between them may be demonstrated with the same exercise solution:

  1. SELECT time_out, DATE_PART('MINUTE', time_out) AS MIN
  2. FROM trip
  3. WHERE DATE_PART('HOUR', time_out) = 12;

Let's proceed to the second exercise. Compound components are not supported by the PostgreSQL EXTRACT function, therefore an ordinary grouping by two components, year and month, may be used:

Solution 4

  1. SELECT EXTRACT(YEAR FROM b_datetime) AS y,
  2. EXTRACT(MONTH FROM b_datetime) AS m, COUNT(*) AS qty
  3. FROM utb
  4. GROUP BY EXTRACT(YEAR FROM b_datetime), EXTRACT(MONTH FROM b_datetime);

Nevertheless, a compound time component can be imitated with the TO_CHAR formatting function:

  1. SELECT TO_CHAR(b_datetime, 'YYYYMM') AS ym, COUNT(*) AS qty
  2. FROM utb
  3. GROUP BY TO_CHAR(b_datetime, 'YYYYMM');

The produced result will be analogous to the one of Solution 3 except the first two columns, which, of course, can be added to the output by including their names in the GROUP BY clause like in Solution 4.

Bookmark and Share
Pages 1 2
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.