转换给出错误

时间:2015-07-07 08:43:31

标签: sql sql-server

当我运行此查询时,我收到错误的语法错误“(”。这是使用CONVERT的正确方法吗?

SELECT
    DATEPART(mm,[actualclosedate]) AS [Month],
    COUNT([opportunityid]) AS 'Won Opportunities',
    CONVERT(SUM(ISNULL([actualvalue],1)) as numeric (10,2)) AS 'Value'

FROM  [dbo].[FilteredOpportunity]
WHERE [owneridname]= @SalesPerson   
    AND YEAR([actualclosedate]) = @Year

GROUP BY 
    DATEPART(mm,[actualclosedate])`

3 个答案:

答案 0 :(得分:3)

而不是CONVERT()使用CAST()函数。

<强>语法

select distinct orgkey
from phone_table p1
where not exists (select 1 from phone_table p2
                  where p1.orgkey = p2.orgkey
                    and p2.preffered_flg = 'Y')

答案 1 :(得分:2)

这应该是正确的陈述:

SELECT
    DATEPART(mm,[actualclosedate]) AS [Month],
    COUNT([opportunityid]) AS 'Won Opportunities',
    CAST(SUM(ISNULL([actualvalue],1)) as numeric (10,2)) AS 'Value'

FROM  [dbo].[FilteredOpportunity]
WHERE [owneridname]= @SalesPerson   
    AND YEAR([actualclosedate]) = @Year

GROUP BY 
    DATEPART(mm,[actualclosedate])`

答案 2 :(得分:2)

如果使用转换,则将类型放在表达式之前:

SELECT
DATEPART(mm,[actualclosedate]) AS [Month],
COUNT([opportunityid]) AS 'Won Opportunities', 
CONVERT(numeric (10,2),
        SUM(
                ISNULL([actualvalue],1)
            )
        ) AS 'Value'

 FROM  [dbo].[FilteredOpportunity]
 WHERE [owneridname]= @SalesPerson   
 AND YEAR([actualclosedate]) = @Year

 GROUP BY 
    DATEPART(mm,[actualclosedate])