Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - Shows dates within the current month [closed]

Tags:

sql

sql-server

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.

like image 451
Sishan Avatar asked Oct 18 '13 16:10

Sishan


People also ask

How do you check if a date is in a certain month in SQL?

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).

How do I get current month data in SQL Server?

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.

Which function in SQL Server can we use to capture the month of a date?

To get a month from a date field in SQL Server, use the MONTH() function. This function takes only one argument – the date.

What does Getdate () function do?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.


1 Answers

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) 
like image 172
Martin Smith Avatar answered Sep 28 '22 18:09

Martin Smith