equivalent function for Month(); DateAdd()

Database

equivalent function for Month(); DateAdd()

Hello,

i want my sql script to automatically query the last month (first to last day) in the database.

Like if i am in august 2016 (the day doesn't matter), the query will pick all datassets with date between the 01/07/2016 and the 31/07/2016

I dno't want the change the range every month in the code and also the ? solution isn't appropriate since i will still have to enter a paremeter.

The script below return an error message saying it s something missing between "where" and "month"

Select T.*

From T

Where Month([Date_attribut]) = Month(DateAdd("m",-1,Date()));

I'm using a Teradata SQL Assistant with ODBC connection.

Thanks in advance for any help.

Regards,

Patrick

1 REPLY
Junior Contributor

Re: equivalent function for Month(); DateAdd()

Hi Patrick,

the Standard SQL way would be EXTRACT(MONTH FROM col), but you better avoid functions on both sides of a comparison: 

Where Date_attribut between TRUNC(CURRENT_DATE, 'mon') AND LAST_DAY(CURRENT_DATE)