Get name of the weekday of a date in Postgresql


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.