TO CHAR(dates)

From SQLZOO
Jump to navigation Jump to search
Compatibility
TO_CHAR(d,'YYYY')
EngineOKAlternative
ingresNoEXTRACT(HOUR from d)
mysqlNoEXTRACT(HOUR from d)
oracleYes
postgresNoEXTRACT(HOUR from d)
sqlserverNoDATEPART(HOUR,d)

TO_CHAR (dates)

TO_CHAR allows you to convert a date to a string using a variety of formats.

 TO_CHAR(d, 'YYYY') -> Four digit year
 TO_CHAR(d, 'MM')   -> Two digit month
 TO_CHAR(d, 'DD')   -> Two digit day
 TO_CHAR(d, 'HH24') -> Two digit hour
 TO_CHAR(d, 'MI')   -> Two digit minutes
 TO_CHAR(d, 'MON')  -> Three character month

In this example you get the month as a three character abbreviation and a four digit year from the date field whn.

SELECT DATEPART(YEAR from whn) AS v
      ,whn
      ,wht
  FROM eclipse
SELECT TO_CHAR(whn, 'MON YYYY') AS v
      ,whn
      ,wht
  FROM gisq.eclipse
SELECT EXTRACT(YEAR from whn) AS v, whn, wht
  FROM eclipse

See also

DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects