Error: DATEPART does not exist, frp, SQL to select last months records

Vereonix picture Vereonix · Dec 1, 2013 · Viewed 20.7k times · Source

I'm trying to get an SQL query to select all records from last month, I have this which from looking numerous places is exactly what I should need, and should work:

SELECT *
FROM orders
WHERE DATEPART(yy,DateOrdered) = DATEPART(yy,DATEADD(m,-1,GETDATE()))
AND DATEPART(m,DateOrdered) = DATEPART(m,DATEADD(m,-1,GETDATE()))

However I keep getting the error:

#1305 - FUNCTION retail.DATEPART does not exist

The query I'm using is word for word from other answers on here, yet I'm getting this error.

Thank you for any help -Tom

Answer

supertopi picture supertopi · Dec 1, 2013

DATEPART is a Transact-SQL function, usable with Microsoft SQL Server. From the question tags, I assume you are using MySQL as your Database Management System.

Take a look at MySQL DATEDIFF