简体   繁体   中英

parsing year from DATE in Oracle SQL

a simple question I'm sure... but i am just drawing a blank on how to get just the ear from a stored DATE in a table. what i am truing to do is generate a query that returns a title, id and the year a course was offered.... and i know i haven't added any thing in the where clause.. here is what i have so far:

select title, id,  To_Char(c.START_DATE,'YYYY') AS year
 from (select c.COURSE_TITLE as title, g.STUDENT_ID as id, c.START_DATE
 from COURSES c, COURSE_INFO g) m;

i have a start date stored, but i only want the year component

I use the format() method for Date objects to pull apart dates. Using Dirk's datetext, here is how I would go about breaking up a date into its constituent parts:

datetxt <- c("2010-01-02", "2010-02-03", "2010-09-10") datetxt <- as.Date(datetxt) df <- data.frame(date = datetxt, year = as.numeric(format(datetxt, format = "%Y")), month = as.numeric(format(datetxt, format = "%m")), day = as.numeric(format(datetxt, format = "%d")))

Which gives:

> df date year month day 1 2010-01-02 2010 1 2 2 2010-02-03 2010 2 3 3 2010-09-10 2010 9 10

There is a Year function in oracle

select title, id,  Year(START_DATE) AS year
 from (select c.COURSE_TITLE as title, g.STUDENT_ID as id, c.START_DATE
 from COURSES c, COURSE_INFO g) m;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM