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

Calculating the Date Difference dynamically

Hi all,

Do me a favour.
I need to calculate the DATEDIFF when we pass the dates dynamiically.

I need query..
Jan 31 '08 #1
9 4470
debasisdas
8,127 Expert 4TB
What is the problem with DATEDIFF function.

Is not it working.
Jan 31 '08 #2
Sorry,i didnt explain the requirement properly!!!
DATEDIFF(dd,SISLS.COLUMN016,SISLS.COLUMN005)/365.

it will get the output.but for a leap year it is not showing properly
Jan 31 '08 #3
debasisdas
8,127 Expert 4TB
Sorry,i didnt explain the requirement properly!!!
DATEDIFF(dd,SISLS.COLUMN016,SISLS.COLUMN005)/365.

it will get the output.but for a leap year it is not showing properly
Instead of hard coding 365 ,you need to find out the number of days in the year and pass the same to the query dynamically.
Jan 31 '08 #4
deepuv04
227 Expert 100+
Instead of hard coding 365 ,you need to find out the number of days in the year and pass the same to the query dynamically.
use it as

DATEDIFF(daySISLS.COLUMN016,SISLS.COLUMN005) >= number of days

or

DATEDIFF(daySISLS.COLUMN016,SISLS.COLUMN005) <= number of days

or

DATEDIFF(daySISLS.COLUMN016,SISLS.COLUMN005) = number of days
Jan 31 '08 #5
ck9663
2,878 Expert 2GB
if the two dates are of different year, which year would you follow? say one of the date is last year (not leap year) and the other date is this year (leap year), what's the number of days that you will use as divisor?

-- ck
Jan 31 '08 #6
if the two dates are of different year, which year would you follow? say one of the date is last year (not leap year) and the other date is this year (leap year), what's the number of days that you will use as divisor?

-- ck
I WIL FOLLOW THE YEAR OF LEAST YEAR
Feb 1 '08 #7
ck9663
2,878 Expert 2GB
I WIL FOLLOW THE YEAR OF LEAST YEAR
assuming SISLS.COLUMN016 is the earlier date between the two.try:


Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(dd,SISLS.COLUMN016,SISLS.COLUMN005)/(datediff(dd,DATEADD(qq, DATEDIFF(qq,0,SISLS.COLUMN016), 0) 
  2. , dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,SISLS.COLUMN016)+1, 0))))
or maybe someone already have a function to get the number of days in a year...

-- ck
Feb 1 '08 #8
debasisdas
8,127 Expert 4TB
TRY TO USE THIS

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION [dbo].[ufn_GetDaysInYear] ( @pDate    DATETIME )
  2. RETURNS INT
  3. AS
  4. BEGIN
  5.  
  6.     DECLARE @IsLeapYear        BIT
  7.  
  8.     SET @IsLeapYear = 0
  9.     IF (YEAR( @pDate ) % 4 = 0 AND YEAR( @pDate ) % 100 != 0) OR
  10.         YEAR( @pDate ) % 400 = 0
  11.         SET @IsLeapYear = 1
  12.  
  13.     RETURN 365 + @IsLeapYear
  14.  
  15. END
  16. GO
  17.  
Feb 1 '08 #9
How to find out the number of days in a financial year.

suppose from date is sep1st,2006.and to date is aug24th,2009.

the entire range involves financial years.i.e.,

april1st,2006-march31st,2007
april1st,2007-march31st,2008
april1st,2008-march31st,2009
april1st,2009-march31st,2010.
we have to calculate the no.of days of last financial year..
waiting for reply..
Feb 4 '08 #10

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

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
10
by: riki | last post by:
hello, i need to calculate num of days between 2 dates... i get separate parts of dates from html form, then i need to "make" begining and ending date and calculate difference between them......
1
by: Tony Williams | last post by:
I have a table with two fields, txtvalue (a number field) and txtmonth ( a date/time field). I want to create a report that shows the difference in value between the value in txtvalue in one value...
6
by: Tony Williams | last post by:
SORRY I know we shouldn't do this but I'm desperate for an answer to this and the previous post didn't seem to get a response. I have a table with two fields, txtvalue (a number field) and...
2
by: Carl | last post by:
Hi, I have a query that produces an output similar to the sample shown below. These records are based on certain criteria and a date range selected from a form. I need to create some kind of...
4
by: Yotam | last post by:
Hi, I need some help with JS. I will be grateful, if you can help me out. I have two date fields (check in, check out) and "number of days" field. I want the script to calculate automatically...
2
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007...
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
6
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
3
by: mfaisalwarraich | last post by:
Hi everybody. I have some problem while calculating time. i have employees who are working on hourly basis. i want to calculate the time. for example if an employee start working at 11:00pm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.