aspose file tools*
The moose likes JDBC and the fly likes SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL" Watch "SQL" New topic
Author

SQL

Vivek Vasudevan
Greenhorn

Joined: Sep 01, 2004
Posts: 5
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"
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Vivek,
Welcome to Javaranch!

I'm moving this to our JDBC forum, since it is about SQL and not Oracle specifically.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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).


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Mani Ram
    Ranch Hand

    Joined: Mar 11, 2002
    Posts: 1140
    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


    Mani
    Quaerendo Invenietis
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: SQL