Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi all

In My Program i Need To get Different between 2 Date ...

I Select The 2 Top Row Correctly With Follow :

SQL
select TOP 2 [pay_date] from  [Peyment] where [pay_sm_id]=2 and [pay_cus_id]=3

With Abow Code I Get These Information :

2012/04/22
2012/04/26


NOW ... i need seperate these Information to 2 part To Calculate Different Between it ...

How Do I ?

In Other Description I Want :

1- Send 2 parameters [pay_sm_id] and [pay_cus_id]
2- Get Diferent Between 2 Date Field ([pay_date]) (Only With Select And DATEDIFF Function)

Please Help Me ...

TNK A LOT
Posted

Hi,
Try this:
SQL
select DATEDIFF(DD,[pay_date],(select [pay_date] from  [Peyment] where [pay_cus_id]=3)) as datediff1 from  [Peyment] where [pay_sm_id]=2

The above query will return Diff of DD, in order to get YY and MM, you need to change DD as DATEDIFF requires 3 parameters.

I hope it helps,
Cheers
 
Share this answer
 
v3
Comments
[no name] 18-Apr-12 11:44am    
Thanks Reza Ahmadi ... But Not Work
Is It Correct That :
Select Top 1 From Top 2
Then
Select Last Record From Top 2
?
If It Is Correct Please take out That For Me Please...
you need this:
=
SQL
Declare @paydate1 datetime
Declare @paydate2 datetime
Select @paydate1 = TOP 2 [pay_date] from  [Peyment] where [pay_sm_id]=2 and [pay_cus_id]=3
Select @paydate2 = TOP 2 [pay_date] from  [Peyment] where [pay_sm_id]=2 and [pay_cus_id]=3 order by [pay_date] desc

select datediff(dd,@paydate1,@paydate2)
 
Share this answer
 
Comments
[no name] 23-Apr-12 1:00am    
ORDER BY DESC sometimes create problem is an indexes based on this column with desc.
Check my solution.
Hi,

Try this solution:

SQL
SELECT DATEDIFF(DAY,
    (SELECT STATUS_DATE FROM(SELECT TOP 2 ROW_NUMBER()OVER(ORDER BY ORGANIZATION_ID DESC)AS [ROWID],[PAY_DATE] FROM  [PEYMENT] WHERE [PAY_SM_ID]=2 AND [PAY_CUS_ID]=3)AS TBL WHERE ROWID = 1),
    (SELECT STATUS_DATE FROM(SELECT TOP 2 ROW_NUMBER()OVER(ORDER BY ORGANIZATION_ID DESC)AS [ROWID],[PAY_DATE] FROM  [PEYMENT] WHERE [PAY_SM_ID]=2 AND [PAY_CUS_ID]=3)AS TBL WHERE ROWID = 2)
)
 
Share this answer
 
Comments
milenalukic 23-Apr-12 10:05am    
Yes I see your point but he asked to only use SELECT and DATEDIFF. I'm assuming this is a school project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900