简体   繁体   English

SQLite:从日期获取星期数

[英]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. 如何从日期获取星期数,在SQL中有一个函数:Datepart(GetDate()),此函数返回星期数。 How can I get this in SQLite? 如何在SQLite中获得此信息?

You can use the strftime function with the format string (1st parameter) as %W 您可以将strftime函数与格式字符串(第一个参数)一起用作%W

as per SQL As Understood By SQLite - Date And Time Functions . 按照SQLite的日期和时间函数理解的SQL

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). 注意日期必须采用公认的日期/时间格式之一(例如,由于日期不是公认的格式,因此2018/03/01将导致空值)。 See the Time Strings section in the link above for the recognised formats. 有关公认的格式,请参见上面链接中的“ 时间字符串”部分。

You can use the appropriate format specifier with strftime() . 您可以将适当的格式说明符与strftime()

See SQLite Date And Time Functions for details. 有关详细信息,请参见SQLite日期和时间函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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