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 2012
. How can I get only the year? I know the YEAR
function can be used, but I'm not sure how.
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;
DATE(date column)
and your proper column name and table name :) –
Ozoniferous CAST
and EXTRACT
if it's TOAD. CAST, EXTRACT –
Ozoniferous This worked for me:
SELECT EXTRACT(YEAR FROM ASOFDATE) FROM PSASOFDATE;
How about this one?
SELECT TO_CHAR(ASOFDATE, 'YYYY') FROM PSASOFDATE
just pass the columnName as parameter of YEAR
SELECT YEAR(ASOFDATE) from PSASOFDATE;
another is to use DATE_FORMAT
SELECT DATE_FORMAT(ASOFDATE, '%Y') from PSASOFDATE;
UPDATE 1
I bet the value is varchar with the format MM/dd/YYYY, it that's the case,
SELECT YEAR(STR_TO_DATE('11/15/2012', '%m/%d/%Y'));
LAST RESORT if all the queries fail
use SUBSTRING
SELECT SUBSTRING('11/15/2012', 7, 4)
DATE_FORMAT
? –
Significant ASOFDATE
? and how is the value formatted? –
Significant M/d/Y
right? try converting it back to date. –
Significant SELECT date_column_name FROM table_name WHERE EXTRACT(YEAR FROM date_column_name) = 2020
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;
DATE(date column)
and your proper column name and table name :) –
Ozoniferous CAST
and EXTRACT
if it's TOAD. CAST, EXTRACT –
Ozoniferous © 2022 - 2024 — McMap. All rights reserved.