473,405 Members | 2,300 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,405 software developers and data experts.

Negative month returns

Hi everyone,

Tried using this SQL below

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    tblIncident.DateObtained, 
  3.    tblIncident.DateOfExpiry, 
  4.    IIf([DateObtained] Is Null 
  5.       Or [DateOfExpiry] Is Null,
  6.          "",
  7.          DateDiff("yyyy",
  8.             [DateObtained],
  9.             [DateOfExpiry])-
  10.                IIf(Format([DateObtained],"mmdd")>
  11.                   Format([DateOfExpiry],
  12.                      "mmdd"),1,0)) 
  13.    AS Years, 
  14.    IIf([DateObtained] Is Null 
  15.       Or [DateOfExpiry] Is Null,
  16.          "",
  17.          IIf(Day([DateObtained])<=Day([DateOfExpiry]),
  18.             DateDiff("m",[DateObtained],[DateOfExpiry])
  19.                -[Years]*12,
  20.             DateDiff("m",[DateObtained],[DateOfExpiry])-[Years]*12-1)) 
  21.    AS Months, 
  22.    IIf([DateObtained] Is Null 
  23.       Or [DateOfExpiry] Is Null,
  24.          "",
  25.          DateDiff("d",DateAdd("m",[Months],
  26.             DateAdd("yyyy",[Years],[DateObtained])),
  27.             [DateOfExpiry])) 
  28.    AS Days 
  29. FROM tblIncident;
A record contains
5/8/2005 - Date1
31/8/1956 - Date2

Date2 - Date1
The query returns me this result:
49 Yrs, -1 Mth, 5 Days

The negative value will appear whenever both date contain same month and date1 day is smaller than date2 days.

Any kind soul knows how to improve this sql.

Thanks very much in advance.
Oct 1 '12 #1
2 1446
zmbd
5,501 Expert Mod 4TB
HappyGala;
That's alot of nested IIF, yea.

Here's what you do...
Take two dates you've had issues with... plug them into your SQL and by hand evaluate the IIF and calcs...

Once you do that, the error in the calculations will become very obvious.
Oct 4 '12 #2
twinnyfo
3,653 Expert Mod 2GB
HappyGala,

Oddly enough, when I use your SQL, here are the results I get:

Expand|Select|Wrap|Line Numbers
  1. DateObtained  DateExpiry  Results
  2. 5/8/2005      31/8/1956   -49, 0, 26
  3. 31/8/1956     5/8/2005    48, 11, 5
  4.  
Which, appears to work....
Oct 5 '12 #3

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

Similar topics

7
by: Matthew Wilson | last post by:
Hi- I just discovered this: >>> -1 // 12 -1 >>> 1 // 12 0 >>>
19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
6
by: James Fortune | last post by:
Several U.S. holidays are defined as the Nth Xday of a given month. For example, Thanksgiving Day is on the 4th Thursday in November. I found the following functions useful in calculating the date...
6
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
8
by: felecha | last post by:
Is there a control that I can put on a Form that would let a user select just the month value? I tried to find a way to make the DateTimePicker or the Month Calendar do what I want but I can't. ...
34
by: prosoft | last post by:
When I use Dim myDTFI As DateTimeFormatInfo = New CultureInfo("he-IL", True).DateTimeFormat Dim strhmon1 As String = (myDTFI.GetMonthName(hmon1)) MsgBox(strhmon1) I get the local name of the...
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
19
by: edfialk | last post by:
Hi, does anyone happen to know of a script that would return the number of seconds in a month if I give it a month and a year? My python is a little weak, but if anyone could offer some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.