(@ nYear int, @ nMonth int)
RETURNS a varchar (16)
AS
The BEGIN
Declare @ nCount int
Declare @ dt smalldatetime
The set @ nMonth=@ nMonth + 1
If (@ nMonth=13)
The set @ nMonth=1
The set @ dt=cast (@ nYear as varchar (4)) + '-' + cast (@ nMonth as varchar (2)) + '1'
The set @ nCount=datepart (day, @ dt - 1)
Return the @ nCount
END
CodePudding user response:
ALTER the FUNCTION [dbo] [fnGetDayCountOfMonth]
(@ nYear int, @ nMonth int) - the incoming two parameters of type int
RETURNS a varchar (16) - RETURNS a varchar type result
AS
The BEGIN
Declare @ nCount INT - define a variable of type INT
Declare @ dt SMALLDATETIME - define a SMALLDATETIME variables of type
Set @ nMonth=@ @ nMonth nMonth + 1 - variables plus 1
If (@ nMonth=13) -- if @ nCount=13, set @ nMonth=1 (here the judgment enough)
Set @ nMonth=1 - if the above through, not @ nMonth=1, don't go through here
Set @ dt=cast (@ nYear as varchar (4)) + '-' + cast (@ nMonth as varchar (2)) + '1' - set @ dt=incoming parameters @ nYear + '-' + @ nMonth + '-' + 1; The final form format, such as the 2019-9-1; Note that there is already in your incoming parameters @ nMonth plus 1 month later,
Set @ nCount=datepart (day, @ dt - 1) - use datepart function to get a line on the @ dt minus a day after the date of, such as the above is the 2019-9-1; Is minus a day after the 2019-8-31; On the basis of the date for this date, the value of the 31, to get the number of days this month,
Return the @ nCount - returns the number of days
END
Written, the individual feels this function is not very serious, there is still room for improvement,
The SELECT dbo. FnGetDayCountOfMonth (2018, 5);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
31
Line (1) affected
CodePudding user response:
This should not MYSQL syntaxCodePudding user response:
Should send in essentially a sectionCodePudding user response:
Thank you for the guidance of theCodePudding user response:
Well, just come in to this also not cooked