The number of days in a month for SQL query. The following describes several methods for obtaining day data and processing of special months in January.
Create function [dbo]. [udf_DaysInMonth]
(
@ Date DATETIME
)
RETURNS INT
AS
BEGIN
Return case when month (@ Date) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
When month (@ Date) IN (4, 6, 9, 11) THEN 30
ELSE [dbo]. [DaysOfFebruary] (YEAR (@ Date ))
END
END
SQL SERVER 2005 obtains the number of days in the current month
SQL code
1. SELECT 32-DAY (getdate () + 32-DAY (getdate ()))
SELECT 32-DAY (getdate () + 32-DAY (getdate ()))
Special handling of January
You need to input a year to obtain the number of days of the year in January. Can be written as a user-defined function
Create function [dbo]. [DaysOfFebruary]
(
@ Year INT
)
RETURNS INT
AS
BEGIN
Return case when (@ Year % 4 = 0 AND @ Year % 100 <> 0) OR (@ Year % 400 = 0) THEN 29 ELSE 28 END
END
Instance
SELECT [dbo]. [DaysOfFebruary] (2011)