加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite 取日期的方法:年,季度,月,当月第几周

发布时间:2020-12-13 00:17:33 所属栏目:百科 来源:网络整理
导读:数据库为 sqlite3 表结构为 tblTask(ID int,TaskName varchar(20),ActionDate datetime) 问:sqlite 通过ActionDate 取季度 的 sql怎么写? 我这里暂时只知道几个例子: 取年份: select * from tblTask where strftime('%Y',ActionDate)='2011' --2011年 取

数据库为 sqlite3
表结构为 tblTask(ID int,TaskName varchar(20),ActionDate datetime)
问:sqlite 通过ActionDate 取季度 的 sql怎么写?

我这里暂时只知道几个例子:
取年份: select * from tblTask where strftime('%Y',ActionDate)='2011' --2011年
取月份: select * from tblTask where strftime('%m',ActionDate)='09'; -- 9月份

取季度?
取当月第几周?

答案如下:

select ActionDate
,round(strftime('%d',ActionDate)/7.0+ 0.495 ) as Week -- 当月第几周
,strftime('%m',ActionDate) AS Month -- 月份
,round(strftime('%m',ActionDate)/3.0 + 0.495) as Season --季度
,strftime('%Y',ActionDate) as Year -- 年份
from tblTask

-----------------------------------------------------------------------------------------

用strftime 函数
%d 日期,01-31
%f 小数形式的秒,SS.SSS
%H 小时,00-23
%j 算出某一天是该年的第几天,001-366
%m 月份,00-12
%M 分钟,00-59
%s 从1970年1月1日到现在的秒数
%S 秒,00-59
%w 星期,0-6 (0是星期天)
%W 算出某一天属于该年的第几周,01-53
%Y 年,YYYY
%% 百分号


select * from tblWord where strftime("%d",updatedate)='26' 表示查询日期为26的数据。比如2012-9-26

SELECT julianday('now') - julianday('1776-07-04'); 表示1776-7-4 距离今天有多少天

http://www.sqlite.org/lang_datefunc.html


select cast(27.33 as int) -- 27
select cast(23.83 as int) -- 24
select round(23.83,0) -- 24
select round(23.8342367,4) -- 23.8342
select round(23.83,4) --23.83


SELECT (0<=cast((julianday('now') - julianday(Updatedate)) as int) and 7>=cast((julianday('now') - julianday(Updatedate)) as int)) as Latest1Week -- 最近1周,(0<=cast((julianday('now') - julianday(Updatedate)) as int) and 31>=cast((julianday('now') - julianday(Updatedate)) as int)) as Latest1Month -- 最近1个月,(0<=cast((julianday('now') - julianday(Updatedate)) as int) and 93>=cast((julianday('now') - julianday(Updatedate)) as int)) as Latest3Month -- 最近3个月,(0<=cast((julianday('now') - julianday(Updatedate)) as int) and 365>=cast((julianday('now') - julianday(Updatedate)) as int)) as Latest1Year -- 最近1年 from tblWord

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读