We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


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.