473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic date on Leap Year

benchpolo
142 100+
I'm trying to write a code that will pick-up the end of the previous month, but I encountered a road block when the month such as February is a leap year. 2011 has 28 days and 2012 has 29 days. Trying to figure out how to incorporate leap year in the code below:

Expand|Select|Wrap|Line Numbers
  1. DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, GETDATE())+1, 0))
Thanks.
Mar 22 '12 #1
6 2495
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. DateAdd(day, -1, CONVERT(VARCHAR(2), Month(GETDATE())) + '/01/' + CONVERT(VARCHAR(4), YEAR(GETDATE())))
Mar 22 '12 #2
ck9663
2,878 Expert 2GB
Here is a list of how to find specific dates in SQL Server.

Happy Coding!!!


~~ CK
Mar 22 '12 #3
PsychoCoder
465 Expert Mod 256MB
If you're trying to determine if it's a leap year then you can use a user defined function. Here's an example on finding if the provided date is a leap year

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.udfIsLeapYear(@date DATETIME)
  2. RETURNS BIT
  3. AS
  4. BEGIN
  5.  
  6.     IF (YEAR(@date) % 4 = 0 AND YEAR(@date) % 100 != 0) OR
  7.         YEAR(@date) % 400 = 0
  8.         RETURN 1
  9.  
  10.     RETURN 0
  11.  
  12. END
  13. GO
Mar 23 '12 #4
ck9663
2,878 Expert 2GB
Here's a simpler one using a built-in-function.


Happy Coding!!!

~~ CK
Mar 23 '12 #5
PsychoCoder
465 Expert Mod 256MB
@ck9663 I don't see how that is simpler than the UDF I provided?
Mar 23 '12 #6
ck9663
2,878 Expert 2GB
I guess it depends on how you look at it.

IsDate is a sql-server date function that returns 1 if the expression you are testing is a valid date and 0 if it is not. No need to build a function.

To illustrate:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select 
  3. IsValidDate_20120229 = isdate('02/29/2012'), 
  4. IsValidDate_20130229 = isdate('02/29/2013')
  5.  
  6.  
and you will have this result

Expand|Select|Wrap|Line Numbers
  1. IsValidDate_20120229 IsValidDate_20130229 
  2. -------------------  --------------------
  3. 1                    0
  4.  
I just think using a built-in function is simpler than creating a new one with case statements.


Happy Coding!!!


~~ CK
Mar 23 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Protoman | last post by:
Hi!!! I need some help on a project I'm that calculates leap years; I'm getting errors that I have no idea what they mean; here's the code: #include <iostream> #include <cstdlib> using...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
5
by: Kasrav | last post by:
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here def year2(): print'This program validates days and months of the...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
4
by: hallsers | last post by:
Here is the problem i am having: - When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any...
1
by: ejrtree15 | last post by:
i am new to java and programming all together. any help would be appreciated. i need to get it to input the date as so: mm/dd/yyyy and then output the date as so: February 29, 2004 is a date in a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.