Obtain the week number of the current year.

Source: Internet
Author: User
1 public static int weekoftheyear ()
2 {
3 // get
4 cultureinfo myci = new cultureinfo ("ZH-CN ");
5 system. Globalization. Calendar mycal = myci. calendar;
6
7 // Date Format
8. calendarweekrule mycwr = myci. datetimeformat. calendarweekrule;
9 dayofweek myfirstdow = myci. datetimeformat. firstdayofweek;
10
11 // this week is week mycal. getweekofyear (datetime. Now, mycwr, myfirstdow)
12 INT thisweek = mycal. getweekofyear (datetime. Now, mycwr, myfirstdow );
13 return thisweek;
14}

In SQL statements, how do I obtain the week of the current year?
For example, I did this to obtain the weekly ranking function.
Table weekstat
Userno int not null,
Author varchar (64) not null,
Year int not null,
Week int not null,
Postcount int
Query statement
Select postcount as returnnum, author
From weekstat
Where (year = datepart (year, getdate () and (week = datepart (Week, getdate ()))
Order by postcount DESC
In this way, we can get the weekly ranking, and the performance is more than a hundred times higher than that of group.
The group by data has timed out. There is no IO operation or CPU usage at all.

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.