EF VAT table Calculating Vat rate on specific date
+--------------+-------------+----------+ | Start Date | End Date | Vat rate | +--------------+-------------+----------+ | Null | 01/01/2011 | 19 | +--------------+-------------+----------+ | 02/01/2011 | 08/08/2011 | 20 | +--------------+-------------+----------+ | 09/08/2011 | Null | 21 | +--------------+-------------+----------+
Thanks for your time
If you are using EF, you can do something like this:
DateTime first = DateTime.Parse("01-01-1900");
DateTime last = DateTime.Parse("01-01-2100");
var rate = db.VatRates.Where(
vr => input > (vr.StartDate ?? first) && input < (vr.EndDate ?? last)).First().Rate;
According to comment below, without EndDate column try this:
DateTime start = DateTime.Parse("01-01-1900");
var rate = db.VatRates.Where(vr => input > (vr.StartDate ?? start))
.OrderByDescending(vr => vr.StartDate).First().Rate;
Works for me :)
Assuming you pass in @Date
SELECT VATRate
FROM VATRateTable
WHERE @Date
BETWEEN ISNULL(StartDate, '1900-01-01') AND ISNULL(EndDate, '2100-01-01')
精彩评论