DATEPART

From SQLZOO
Jump to navigation Jump to search
Compatibility
DATEPART(YEAR, d)
EngineOKAlternative
ingresNoEXTRACT(YEAR FROM d)
mysqlNoEXTRACT(YEAR FROM d)
oracleNoEXTRACT(YEAR FROM d)
postgresNoEXTRACT(YEAR FROM d)
sqlserverYesDATEPART(YEAR, d)

DATEPART

DATEPART allows you to retrieve components of a date.

You can extract also YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.

   DATEPART(YEAR, d)    DATEPART(MONTH, d)
   DATEPART(DAY, d)     DATEPART(HOUR, d)
   DATEPART(MINUTE, d)  DATEPART(SECOND, d) 
schema:gisq

In this example you get the year and the month from the date whn.

SELECT whn
      ,DATEPART(YEAR, whn)  AS yr
      ,DATEPART(MONTH, whn) AS mnth
  FROM eclipse
SELECT whn
      ,EXTRACT(YEAR FROM td)  AS yr
      ,EXTRACT(MONTH FROM td) AS mnth
  FROM gisq.eclipse
SELECT whn
      ,EXTRACT(YEAR FROM td)  AS yr
      ,EXTRACT(MONTH FROM td) AS mnth
  FROM eclipse

See also

Language:Project:Language policy English  • Deutsch
DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects