Several methods of SQL to get the number of days in the current month

Original: Several methods of SQL to get the number of days in the current month

The date directly minus the number of type int is equal to DATEADD(DAY,- number, date)

The following three methods:

1. The date plus one month minus the current number of days is equivalent to the date of the last day of the month. Then get the number of days. (Note that this method cannot be used: the current date minus the current number of days, plus one month, the new date is not necessarily the date of the last day of the current month. For example, the current month is March. Error: select day(dateadd(month, 1,getdate()-day(getdate()))))
select day(dateadd(mm,1,getdate())-day(getdate()))

2. Convert converts the date to 120 format in the format of "2011-1-1".

     Finally got the last day of the month. Then get the number of days.

select day( dateadd(day,-1, dateadd(month,1,convert(char(07),getdate(),120)+'-01')) ),

3. The difference between today's day in the next month and today's date. (dates left in this month + dates that have passed)

select datediff(dd , GETDATE(), dateadd(mm, 1,GETDATE()))

4, the following method is very sharp.

      First get the last day of the previous month, and then add a number of days (marked in black), as long as this number of days is greater than the number of days in a month. But not more than the number of days in two months.

      This will generate the next month's date. Then subtract the extra days from the newly generated date from this number of days, which is the number of days in the current month.

select 32-Day(getdate()+(32-Day(getdate())))
 5,同上
select 50-DAY(GETDATE()-DAY(GETDATE())+50)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523458&siteId=291194637