Sorry to be a bother guys. I have been tasked with some code review at a client location and the laptop issued to me does not have SQL installed. While I am waiting for the installation to happen, wanted to get busy looking at the code and came across this gem
CREATE FUNCTION [dbo].[Uf_GetTotalDaysInMonth]
(
-- Add the parameters for the function here
@anydateofMonth datetime
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @totalDaysInMonth int
-- Add the T-SQL statements to compute the return value here
DECLARE @givendate datetime
SET @givendate = STR(Year(@givendate)) + '-' + STR(Month(@givendate) + 1) + '-01'
select @totalDaysInMonth = datepart(dd, dateadd(day, -1, @givendate))
-- Return the result of the function
RETURN @totalDaysInMonth
END
Ignoring the use of needless extra variable, I believe that this function will crash in December
STR(Month(@givendate) + 1)
will evaluate to 13 and will give an out of scope date error. Could someone please validate this for me?
You vill get error in your function when pass @anydateofMonth December date.
You can use this: