sqlite 取日期

数据库为 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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值