Skip to content
Advertisement

How to see if date is in current month SQL Server?

I have a Ticket_Date column that is in the format YYYY-MM-DD HH:MI:SS

I want to check if the Ticket_date is in the current month.

So far I have :

 Ticket_date >= '2015-04-01' and Ticket_date < '2015-04-30'

but I need it to work with the current month rather then hardcoded

Advertisement

Answer

 YEAR(Ticket_date) = YEAR(getdate()) and 
MONTH(Ticket_date) = MONTH(getdate())

but i would also save current date in variable to guarantee what result of getdate() doesn’t change when executing long query at midnight

declare @today datetime = getdate()
...
 YEAR(Ticket_date) = YEAR(@today) and 
MONTH(Ticket_date) = MONTH(@today)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement