简体   繁体   中英

SQLite: Get week number from date

How can I get week number from date, in SQL there is a function: Datepart(GetDate()) and this return the number of week. How can I get this in SQLite?

You can use the strftime function with the format string (1st parameter) as %W

as per SQL As Understood By SQLite - Date And Time Functions .

Example

DROP TABLE IF EXISTS getweek;
CREATE TABLE IF NOT EXISTS getweek (mydate TEXT);
INSERT INTO getweek VALUES ('2018-01-01'),('2018-02-01'),('2018-03-01');
SELECT *, strftime('%W',mydate) AS weekofyear FROM getweek;

Which results in :-

在此处输入图片说明

Note The date has to be in one of the recognised date/time formats (eg 2018/03/01 would result in null as the date is not in recognised format). See the Time Strings section in the link above for the recognised formats.

You can use the appropriate format specifier with strftime() .

See SQLite Date And Time Functions for details.

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