SQL Server: compare dates by month and day only

I have a stored procedure that retrieves records based on dates corresponding to the input date, which still works. Both dates in the table and my input date are formatted as date and time.

Instead of comparing full dates, I would like to change this so that it only compares the month and day, so that it works with any year for input.

Example: The date in the table is saved as 2013-04-30, and my input date 2014-04-30. I want the stored procedure to still return this record, independent of the year, for the month and day.

My stored procedure:

ALTER PROCEDURE [dbo].[FetchDays]
    @inputDate datetime
AS
BEGIN
    SET NOCOUNT ON;
    SELECT      dateID,
                dayDT,
                countries,
                regions
    FROM        DaysDT
    WHERE       dayDT = @inputDate
    FOR XML PATH('daysFixed'), ELEMENTS, TYPE, ROOT('root')

END

Thanks so much for any help with this, Mike.

+4
2

- :)

ALTER PROCEDURE [dbo].[FetchDays]
    @inputDate datetime
AS
BEGIN
    SET NOCOUNT ON;
    SELECT      dateID,
                dayDT,
                countries,
                regions
    FROM        DaysDT
    WHERE       
        DAY(dayDT) = DAY(@inputDate) --Extract and compare day
        AND MONTH(dayDT) = MONTH(@inputDate) --Extract and compare month
    FOR XML PATH('daysFixed'), ELEMENTS, TYPE, ROOT('root')

END
+8

:

WHERE datepart(day, dayDT) = datepart(day,@inputDate)
AND datepart(month, dayDT) = datepart(month,@inputDate)

, .

+2

Source: https://habr.com/ru/post/1538207/


All Articles