如何在SQL Server中将Year(GETDATE())转换为int?

时间:2017-09-09 19:20:45

标签: sql sql-server date stored-procedures

我想将YEAR(GETDATE()), MONTH(GETDATE())转换为int

这是我的查询

DECLARE @StartDate DATE
DECLARE @EndDate DATE
DECLARE @EndOfMonth Date

SET @StartDate = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
SET @EndDate = GETDATE() - 1

INSERT INTO MyTable (StoreID, ItemID, Year, Month, Qty, ExtPrice, ExtCost)
    SELECT
        Storeid, ItemID,
        YEAR(Time) Years, MONTH(Time) Months,
        SUM(Qty), SUM(ExtendedPrice),
        SUM(ExtendedCost)
    FROM
        Records
    WHERE
        Time >= @StartDate AND Time <= @EndDate
    GROUP BY
        Storeid, ItemID, 
        YEAR(Time), MONTH(Time) 

我收到错误:

  

操作数类型冲突:日期与int

不兼容

当我为上述查询创建存储过程时,它会抛出上述错误。

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码

git fetch

答案 1 :(得分:0)

你应该把你的年份投到国内

CAST(Year(GETDATE()) AS int)