选择要用于LIKE的输出列

时间:2014-01-31 14:15:04

标签: database tsql select sql-like

我想将 LIKE 用于我的 CONVERT 输出的列,就像下面的代码一样。 但它没有用,因为我选择的列不属于我一直使用的视图。我有办法这样做吗?或者我真的要创建一个新视图吗?

SELECT 
[DESCRIPTION],
CONVERT(varchar, CAST([ITEM VALUE]*[QTY] AS money), 1) AS [TOTAL AMOUNT],
FROM vwItemList
WHERE [TOTAL AMOUNT] LIKE '%' + @targetItem + '%'

1 个答案:

答案 0 :(得分:2)

您必须使用子查询或重复它,因为您无法引用WHERE - 子句中的别名

SELECT 
[DESCRIPTION],
CONVERT(varchar, CAST([ITEM VALUE]*[QTY] AS money), 1) AS [TOTAL AMOUNT],
FROM vwItemList
WHERE CONVERT(varchar, CAST([ITEM VALUE]*[QTY] AS money), 1) LIKE '%' + @targetItem + '%'

另一个选项,CTE类似于子查询:

WITH CTE AS
(
    SELECT [Description], 
           CONVERT(VARCHAR, Cast([Item value] * [Qty] AS MONEY), 1) AS [TOTAL AMOUNT], 
    FROM   Vwitemlist 
)
SELECT [Description],
       [TOTAL AMOUNT]
FROM   CTE 
WHERE  [TOTAL AMOUNT] LIKE '%' + @targetItem + '%'