Reputation: 339
I want to code a function that take a year as parameter and return the person that born in that year. But my people table format like 1993-01-06 00:00:00 so how can i compare date and return the filtered person in sql?
Upvotes: 0
Views: 48
Reputation: 167832
Assuming that your table has a column with the DATE
data type then:
SELECT *
FROM table_name
WHERE date_column >= TO_DATE( :year || '-01-01', 'YYYY-MM-DD' )
AND date_column < TO_DATE( ( :year + 1 ) || '-01-01', 'YYYY-MM-DD' );
(Use a range of dates rather than using EXTRACT
or TO_CHAR
on your date column as this allows Oracle to use an index on the date column.)
If your column has a string data type with a 4-digit year:
SELECT *
FROM table_name
WHERE string_column LIKE :year || '-%'
(Don't just concatenate %
as, if you have historical years then, trying to match the year 200
would also match the year 2000
.)
Upvotes: 2
Reputation: 1269513
If the data type of a the column is a string, then you can use:
where col like :year || '%'
If the data type is a date or timestamp, then you can use:
where extract(year from col) = :year
Upvotes: 0