• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know whether there is any readymade function in MS SQL for finding out whether the year is a leap one or not. Thanks in Advance!


Vivek V
"Today is the first day of the rest of your life"
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
Welcome to Javaranch!

I'm moving this to our JDBC forum, since it is about SQL and not Oracle specifically.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can just get the year and divide it by 4. if you get the reminder it means the year is not a leap year, else it is.

i think we have a function called mod(). you can use it to in order to get the reminder.

any particular function. sorry i dont know. may be someone else.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite. 1900, 1800, 1700 aren't Leap Years for example, though they all divide by 4. The rules are, its a Leap Year if:
  • The year divides by 4, unless
  • The year also divides by 100, unless
  • The year divides by 400.

  • Assuming you are talking about the Gregorian Calendar that is. Ugly, true, buth those are the rules. And its far easier than trying to work out when Easter is...

    Can't think of any way to do it in SQL, without using functions. Easier is to do just return the date into the Java layer and work it out there (since GregorianCalendars have the isLeapYear() method).
     
    Ranch Hand
    Posts: 1140
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by adeel ansari:
    you can just get the year and divide it by 4. if you get the reminder it means the year is not a leap year, else it is.



    Works fine if you are using a Julian calendar, but not with a Gregorian calendar (one which is widely used)
    For example the year 1900 is divisible by 4, but is not a leap year, if you are following the Gregorian calendar.

    There should be an additional check there
    If the year is divisible by 100, it should also be divisible by 400. If it doesn't, then it is not a leap year
     
    Adeel Ansari
    Ranch Hand
    Posts: 2874
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry folks I dont know about this. it is ridiculus. i was sleeping some where out of this world.
    [ September 22, 2004: Message edited by: adeel ansari ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic