Determining if Year is a Leap Year...

Moderator: NorbertKrupa

Post Reply
sarah
Intermediate
Intermediate
Posts: 77
Joined: Mon Aug 27, 2012 1:34 pm

Determining if Year is a Leap Year...

Post by sarah » Wed Nov 14, 2012 3:10 pm

Hello,

What's the best way to determine if a year is a leap year in Vertica?

Here is what I was thinking for a function:

Code: Select all

CREATE OR REPLACE FUNCTION is_leapyear(d timestamp)
RETURN BOOLEAN
AS BEGIN
  RETURN DECODE(DAY(LAST_DAY(TO_DATE(year(d) || '-02-01', 'YYYY-MM-DD'))), 29, TRUE, FALSE);
END;
Example run ...

Code: Select all

dbadmin=> SELECT sysdate, is_leapyear(sysdate);
          sysdate           | is_leapyear
----------------------------+-------------
 2012-11-14 09:09:14.037244 | t
(1 row)

Is this the most (or at least an) efficient method?

Thanks!
Have a GREAT day!

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Determining if Year is a Leap Year...

Post by id10t » Wed Nov 14, 2012 4:18 pm


sarah
Intermediate
Intermediate
Posts: 77
Joined: Mon Aug 27, 2012 1:34 pm

Re: Determining if Year is a Leap Year...

Post by sarah » Wed Nov 14, 2012 5:06 pm

Thanks, sKwa!!! That is a VERY useful link! I'll create those is_leap_year functions in our environment :D
Have a GREAT day!

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Determining if Year is a Leap Year...

Post by id10t » Wed Nov 14, 2012 5:58 pm

Hi!

Only one advice: don't trust to anyone, do tests, check everything you take from others, especially be careful with production environment.
(like a small child :-) but it's not joke)

Post Reply

Return to “New to Vertica SQL”