SterlinkArcher
SterlinkArcher

Reputation: 731

SQL-Server combing datepart with getdate and dateadd

I'm working on making a Stored procedure in SQL-server 2008 r2. Already got the SELECT and all the things done, but having some problems combining my datepart with a Dateadd and getdate. The thing is I want to show it like this in the query.

2013-06 2013-07 2013-08

So I try to use this code but I keep getting invalid syntax...

and Datepart(yy, Column) = 2013

But I dont want it to be hardcoded, since the people using the system should just be able to print it each month. That was why I was thinking getdate-1

DATEPART(MM,DATEADD(MM,-1 GETDATE())

I want to combine the hardcode and change it into the lower one. The SELECT code looks like this at the moment.

INSERT INTO #EXCEL(COL1,COL2,COL3)
SELECT SUM (AVERAGE) / COUNT DISTINCT(TABLE.COLUMN)
FROM TABLE
INNER JOIN TABLE1 ON TABLE2.COLUMN = BLA BLA 
WHERE SOMETHING = X
AND DATEPART(YY,COLUMNDATE) = 2013
AND DATEPART(MM,COLUMNDATE) = 8

I want to use my getdate combined, but I keep getting syntax error.

Upvotes: 0

Views: 2158

Answers (2)

Devart
Devart

Reputation: 121922

Try this one -

--SELECT SUM(AVERAGE) / COUNT(DISTINCT t.[COLUMN])
--FROM dbo.[TABLE] t
--JOIN dbo.TABLE1 t2 ON t2.[COLUMN] = 'BLA BLA...' 
--WHERE SOMETHING = X
    AND YEAR(COLUMNDATE) = YEAR(GETDATE())
    AND MONTH(COLUMNDATE) = MONTH(GETDATE())

Upvotes: 2

SterlinkArcher
SterlinkArcher

Reputation: 731

So this is the answer for those of you who have the same troubles.

AND DATEPART(YY,COLUMNDATE) = DATEPART(YY,DATEADD(MM, -1, GETDATE()))

Upvotes: 1

Related Questions