15,797,721 members
See more:
I need to calculate date difference using flowing query but it does not work. Please advise.

SQL
```SELECT ID, VoucherNo, DateRec, ProductID, Description, ClaimantCo, ClaimantName, City, Phone, DateReturn, ReqJob, WSIns, Remarks, Status, Part1, Part2, Part3, Part4, OtherWSCharges, FDCharges, P1Charges, P2Charges, P3Charges, P4Charges, DateJobBegun, DateJobDone, WSRemarks, TotChargesFromWS, DeliveryStatus, DateDelivered FROM tblClaims
WHERE DateDiff(dd, DateReturn, GETDATE()) =3```

My purpose is to get records for which three days are left to DateRetun, comparing to today's date.

Thanks
Posted
Updated 14-Feb-12 23:54pm
v2
Simon_Whale 15-Feb-12 5:26am
In what way doesn't it work? no results? wrong results?

How is the DateReturn setup?

## Solution 1

Try reversing the order:
SQL
```SELECT ID, VoucherNo, DateRec, ProductID, Description, ClaimantCo, ClaimantName, City, Phone, DateReturn, ReqJob, WSIns, Remarks, Status, Part1, Part2, Part3, Part4, OtherWSCharges, FDCharges, P1Charges, P2Charges, P3Charges, P4Charges, DateJobBegun, DateJobDone, WSRemarks, TotChargesFromWS, DeliveryStatus, DateDelivered FROM tblClaims
WHERE DateDiff(dd, GETDATE(), DateReturn) =3```
Your way returns when it is 3 days overdue...

Furqan Sehgal 15-Feb-12 5:35am
Thanks ! the error is
Generated Select Command
Failed to get Schema for the table

## Solution 2

First of all, take a look here[^] and here[^]

SQL
```SELECT [ID], GETDATE() AS [ThisDay], [DateReturn], CONVERT(INT, GETDATE() - [DateReturn]) AS [DayDifference]
WHERE DATEDIFF(day, [DateReturn],GETDATE()) =3
ORDER BY [ID]```

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

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 120 Maciej Los 90 Richard MacCutchan 70 Andre Oosthuizen 50 Richard Deeming 40
 Richard Deeming 538 Andre Oosthuizen 360 Maciej Los 350 Dave Kreskowiak 335 Richard MacCutchan 243

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