简体   繁体   中英

get week number of a date

SELECT strftime('%W', day) AS week, sum(amount) FROM MyTable GROUP BY week ORDER BY week DESC

This query is useful to get the amount by week. But this one gives week number in year, May I know how to get week number in month. I have googled lot. But all results are related to week number in year not in month.

Thanks in advance.

You can try this code

Create A Calendar Instance from the Sqlite date parts

Calendar now = Calendar.getInstance();


  now.set(Calendar.YEAR,2013);
  now.set(Calendar.MONTH,04);//0- january ..4-May
  now.set(Calendar.DATE, 04);

System.out.println("Current week of month is : " +
            now.get(Calendar.WEEK_OF_MONTH));

System.out.println("Current week of year is : " +
            now.get(Calendar.WEEK_OF_YEAR));

You can manipulate strftime() to get your answer.

Apply strftime() on given date and FIRST date of given dates month and subtract them to get your week number of month

Eg

 SELECT  strftime('%W','2013-05-04')-strftime('%W','2013-05-01') +1;

SQL Fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM