All Forums Database
plechat 1 post Joined 08/16
31 Aug 2016
equivalent function for Month(); DateAdd()

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.

dnoeth 4628 posts Joined 11/04
31 Aug 2016

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)



You must sign in to leave a comment.