I'm just new to sql server query. I have a column named duedate. I want to filter duedate data within the current month.
SELECT * FROM TABLE1 WHERE duedate = within current month only
Thanks in advance.
Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column. (In our example, we use the start_date column of date data type).
We can retrieve the current month value in SQL using the MONTH() and DATEPART() functions along with the GETDATE() function. To retrieve the name of the month functions in SQL such as DATENAME() and FORMAT() are used.
To get a month from a date field in SQL Server, use the MONTH() function. This function takes only one argument – the date.
The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.
You should use a range query rather than wrapping the column in a function call so that an index may be used.
SELECT *
FROM TABLE1
WHERE duedate >= DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
AND duedate < DATEADD(month, 1 + DATEDIFF(month, 0, getdate()), 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With