SQL query for extracting year from a date

前端 未结 5 923
忘了有多久
忘了有多久 2021-02-04 12:17

I am trying to create a query that gets only the year from selected dates. I.e. select ASOFDATE from PSASOFDATE; returns 11/15/2012, but I want only

5条回答
  •  无人共我
    2021-02-04 12:42

    Edit: due to post-tag 'oracle', the first two queries become irrelevant, leaving 3rd query for oracle.

    For MySQL:

    SELECT YEAR(ASOFDATE) FROM PASOFDATE
    

    Editted: In anycase if your date is a String, let's convert it into a proper date format. And select the year out of it.

    SELECT YEAR(STR_TO_DATE(ASOFDATE, '%d-%b-%Y')) FROM PSASOFDATE
    

    Since you are trying Toad, can you check the following code:

    For Oracle:

    SELECT EXTRACT (TO_DATE(YEAR, 'MM/DD/YY') FROM ASOFDATE) FROM PSASOFDATE;
    

    Reference:

提交回复
热议问题