KANEDA 0149
asked on
SQL Case Statement on Any Quarter End Date
I need help on a case when statement. I am pulling a query of invoices based on the invoice date from and invoice date to range. If the invoice date is any quarter end date (i.e. 3/31/2014, 6/30/2014, 9/30/2014, 12/31/2014, 3/31/2015, 6/30/2015, etc...) then set value as ‘Mgmt Fee’ else all other non-quarter end invoice dates (i.e. 4/1/2014, 4/29/2014, etc…) to ‘Final Fee’ end as TYPE.
Below is a sample of my SQL statement. Any help on this without using a function or stored procedure against the database would be awesome. Is it possible to do this as a select or creating a #temp table on each run?
Below is a sample of my SQL statement. Any help on this without using a function or stored procedure against the database would be awesome. Is it possible to do this as a select or creating a #temp table on each run?
select INVOICEDATE, ACCOUNT, FEE
, case when INVOICDATE in [QtrEndDate] then ‘Mgmt Fee’ else ‘Final Fee’ end as TYPE
from INV_INVOICESUMMARY
where INVOICEDATE >= @FromDate
and INVOICDATE DATE <= @ToDate
There are other ways to do it, like creating a date dimension table, but this should work and have decent performance:
SELECT
INVOICEDATE,
ACCOUNT,
FEE
, CASE WHEN
(MONTH(INVOICDATE) = 3 AND DAY(INVOICDATE) = 31)
OR (MONTH(INVOICDATE) = 6 AND DAY(INVOICDATE) = 30)
OR (MONTH(INVOICDATE) = 9 AND DAY(INVOICDATE) = 30)
OR (MONTH(INVOICDATE) = 12 AND DAY(INVOICDATE) = 31)
THEN 'Mgmt Fee' ELSE 'Final Fee' END
AS [Type]
FROM INV_INVOICESUMMARY
WHERE INVOICEDATE >= @FromDate
AND INVOICDATE DATE <= @ToDate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much Randy, that worked exactly as I needed.
, case when exists (select end_of_quarter_date from dbo.f_table_date(@FromDate
from INV_INVOICESUMMARY i
where INVOICEDATE >= @FromDate
and INVOICDATE DATE <= @ToDate
Open in new window