1. Determine how many days a certain year
Implementation principle: First use DateDiff to obtain the first day of the current month, and then add a month to get the first day of next month, then subtract 1 minutes, then take the date
Number of days, that is, the maximum date of the month, or the number of days of the month
CREATE FUNCTION daysinmonth (@date datetime) Returns int
As
BEGIN
Return Day (DateAdd (Mi,-3,dateadd (M, DATEDIFF (m,0, @date) +1,0))
End
Call Example:
SELECT dbo. DaysInMonth (' 2006-02-03 ')
(2) Calculate which day is Monday of the week
SELECT DATEADD (Week, DATEDIFF (week, ' 1900-01-01 ', GETDATE ()), ' 1900-01-01 ')--return to 2006-11-06 00:00:00.000
Or
SELECT DATEADD (Week, DATEDIFF (Week,0,getdate ()), 0)
(3) The first day of the current quarter
SELECT DATEADD (Quarter, DATEDIFF (Quarter,0,getdate ()), 0)-back to 2006-10-01 00:00:00.000
(4) How many days a quarter
Declare @m tinyint, @time smalldatetime
Select @m=month (getdate ())
Select @m=case when @m between 1 and 3 then 1
When @m between 4 and 6 then 4
When @m between 7 and 9 then 7
Else Ten End
Select @time =datename (year,getdate ()) + '-' +convert (varchar (), @m) + '-01 '
Select DateDiff (Day, @time, DateAdd (mm,3, @time))-return 92