I need to simplify a query because the parts are often reusable & I'd rather change it in one place than go into each part & update it. I know that I can't store a query as a variable unless the query only results in 1 row of data. Enclosed is a small excerpt of my query which is pretty easy to understand.For several operations it has to ask itself if data appears in one column and if not, if it should pull the data from the second column. Since these conditional searches are confusing and appear regularly I was wondering if I could store them differently in one centralized place. ,CASE WHEN oi.[AverageCost]='0' THEN CONVERT(DECIMAL(18,2), o.[GrandTotal] - ((oi.[SiteCost] * oi.Qty) + (CASE WHEN ISNULL(bO.Commission,0) > ISNULL(o.FinalValueTotal,0) THEN ISNULL(bO.Commission,0) ELSE ISNULL(o.FinalValueTotal,0) END + ISNULL(oi.PostingFee,0) + ISNULL(o.paychannelFeeTotal,0)+ ISNULL(p.[channelTransactionFee],0)) + ISNULL(o.[FinalShippingFee],0))) - ISNULL(o.TaxTotal,0) + (CASE WHEN O.TimeOfOrder BETWEEN eS.startDate AND eS.endDate THEN ISNULL(eS.subsidyAmount,0) * oi.Qty ELSE '0' END) ELSE CONVERT(DECIMAL(18,2), o.[GrandTotal] - ((oi.[AverageCost] * oi.Qty) + (CASE WHEN ISNULL(bO.Commission,0) > ISNULL(o.FinalValueTotal,0) THEN ISNULL(bO.Commission,0) ELSE ISNULL(o.FinalValueTotal,0) END + ISNULL(oi.PostingFee,0) + ISNULL(o.paychannelFeeTotal,0)+ ISNULL(p.[channelTransactionFee],0)) + ISNULL(o.[FinalShippingFee],0))) - ISNULL(o.TaxTotal,0) + (CASE WHEN O.TimeOfOrder BETWEEN eS.startDate AND eS.endDate THEN ISNULL(eS.subsidyAmount,0) * oi.Qty ELSE '0' END) END
-SergioI use Microsoft SQL 2008