I have a stored procedure that fetches records based on dates matching a date input which works fine so far. Both the dates in the table and my input date are formatted as datetime.
Instead of comparing the full dates I would like to change this so that it only compares month and day so that it works with any year for the input.
Example:
A date in the table is saved as 2013-04-30
and my input date is 2014-04-30
.
What I want is that the stored procedure still returns that record independent of the year as long as month and day match.
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
Many thanks for any help with this, Mike.
To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.
Solution 1. Like operator will not work with DateTime.
You can do something like this ;)
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
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