[Original] How many working days does SQL Server obtain in a month of a year (excluding Sunday only)

Source: Internet
Author: User
-- Calculate the number of Sundays in a month of a year (excluding Saturday) Create Function f_getnums (@ year_month varchar (8) returns INTAS begin declare @ BDT datetime, @ EDT datetime, @ I int set @ I = 0 set @ BDT = cast (@ year_month + '-01' As datetime) set @ EDT = dateadd (D,-1, dateadd (month, 1, cast (@ year_month + '-01' as datetime) while datediff (D, @ BDT, @ EDT)> = 0 begin if -- datepart (DW, @ BDT) = 1 or datepart (DW, @ BDT) = 7 begin set @ I = @ I + 1 end set @ BDT = dateadd (d, 1, @ BDT) end return @ I enddeclare @ date datetime set @ date = '2017-12-01 '-- 1. select datediff (DD, @ date, dateadd (mm, 1, @ date) -- 2. obtain the number of Sunday select DBO instances in a month in a year. f_getnums (convert (varchar (07), @ date, 120) as workday -- 3. obtain the number of working days of a month in a year (excluding Sunday only) Select datediff (DD, @ date, dateadd (mm, 1, @ date)-DBO. f_getnums (convert (varchar (07), @ date, 120) as workday

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.