EXTRACT

From SQLZOO
Revision as of 11:11, 13 July 2012 by Connor (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Compatibility
EXTRACT(YEAR FROM d)
EngineOKAlternative
ingresYesEXTRACT(YEAR FROM d)
mysqlYesYEAR(d)
oracleYesTO_CHAR(d,'YYYY')
postgresYesYEAR(d)
sqlserverNoDATEPART(YEAR, d)

EXTRACT

EXTRACT allows you to retrieve components of a date.

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

  EXTRACT(YEAR FROM d)    EXTRACT(MONTH FROM d)
  EXTRACT(DAY FROM d)     EXTRACT(HOUR FROM d)
  EXTRACT(MINUTE FROM d)  EXTRACT(SECOND FROM d)

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

SELECT whn
      ,DATEPART(YEAR, whn)  AS yr
      ,DATEPART(HOUR, whn) AS hr
  FROM eclipse
SELECT whn
      ,EXTRACT(YEAR FROM td)  AS yr
      ,EXTRACT(HOUR FROM td) AS hr
  FROM eclipse

See also

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense