Posted By

arunpjohny on 10/16/07


Tagged

postgres


Versions (?)

Get name of the weekday of a date in Postgresql


 / Published in: SQL
 

  1. CREATE OR REPLACE FUNCTION dayOfWeek (date Date) RETURNS Character Varying(15) AS $$
  2. DECLARE
  3. DAY_OF_WEEK_CONST Character Varying(15) := 'dow';
  4.  
  5. dayOfWeek Integer := 0;
  6. dayName Character Varying(15) := 'Test';
  7. BEGIN
  8.  
  9. dayOfWeek := date_part(DAY_OF_WEEK_CONST, Date);
  10.  
  11. IF dayOfWeek = 0 THEN
  12. dayName := 'Sunday';
  13. ELSEIF dayOfWeek = 1 THEN
  14. dayName := 'Monday';
  15. ELSEIF dayOfWeek = 2 THEN
  16. dayName := 'Tuesday';
  17. ELSEIF dayOfWeek = 3 THEN
  18. dayName := 'Wednesday';
  19. ELSEIF dayOfWeek = 4 THEN
  20. dayName := 'Thursday';
  21. ELSEIF dayOfWeek = 5 THEN
  22. dayName := 'Friday';
  23. ELSEIF dayOfWeek = 6 THEN
  24. dayName := 'Saturday';
  25. END IF;
  26. RETURN dayName;
  27. END;
  28. $$ LANGUAGE plpgsql;

Report this snippet  

You need to login to post a comment.