如果您有约会,那么只需:
select day(eomonth(date))
Run Code Online (Sandbox Code Playgroud)
如果您有月份编号,那么:
select day(eomonth(datefromparts(2020, month_number, 1)))
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以使用:
select day(eomonth ('2018-02-01')) as NoOfDays
Run Code Online (Sandbox Code Playgroud)
结果将是:
NoOfDays
-----------
28
Run Code Online (Sandbox Code Playgroud)
如果您有约会并且在 2012 年或之后:
SELECT day(eomonth(yourdate))
Run Code Online (Sandbox Code Playgroud)
在处理二月时,月份名称/数字很容易出错——你认为它是 28 还是 29,你在计算时指的是哪一年等等。