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

SQLite中将字符串相加

比如把’abc’ 和’de’连接成’abcde’,用 || 就可以实现:

select ‘abc’||’de’ ;

sqlite 截取字符串函数substr

函数:substr(string string,num start,num length)

Sqlite获取字符串长度

select mkey,LENGTH(mkey) from mword where LENGTH(mkey) =1