Posted By

arunpjohny on 10/16/07


Tagged

postgres


Versions (?)

Function to get year from a date in Postgresql


 / Published in: SQL
 

This kind of functions will be use full if we are migrating a database from one provider to another like from MS SQL Server to Postgresql.

  1. CREATE OR REPLACE FUNCTION year (date1 Date) RETURNS integer AS $$
  2. DECLARE
  3. YEAR_CONST Character Varying(15) := 'year';
  4.  
  5. yearPart Integer := 0;
  6. yearPartInDoublePrecision Double Precision := 0;
  7. BEGIN
  8.  
  9. yearPartInDoublePrecision := date_part(YEAR_CONST, date1);
  10.  
  11. yearPart := CAST(yearPartInDoublePrecision AS Integer);
  12. RETURN yearPart;
  13. END;
  14. $$ LANGUAGE plpgsql;

Report this snippet  

You need to login to post a comment.