SQLite一些函数用法

时间:2020-12-08 14:04:12
--格林威治日期时间,比北京时间晚8小时
select datetime('now'); --格林威治日期
select date('now'); --本地时间
select time('now','localtime'); --日期时间格式化
select strftime('%Y-%m-%d %H:%M:%S','now','localtime'); --加1小时
select datetime(date('now'),'+1 hour'); --加1小时30分钟
select datetime(date('now'),'+1 hour','+30 minute'); --当月最后一天
select date('now','localtime','start of month','+1 month','-1 day'); --当月1号
select date('now','localtime','start of month'); --下月1号
select date('now','localtime','start of month','+1 month'); --截取字符串(第2个参数为从1算起的开始位置,第3个位置为截取长度):结果为123
select substr('abc123',4,3); --计算长度,结果为6
select length('abc123'); --返回小写、大写,结果为abc,ABC
select lower('abC'),upper('abC'); --四舍五入保留2位小数
select round(cast(1 as double)/cast(3 as double),2); --case when用法
select
case
when cast(strftime('%H','now','localtime') as int) >= 6 and cast(strftime('%H','now','localtime') as int) <=12 then '上午'
when cast(strftime('%H','now','localtime') as int) >12 and cast(strftime('%H','now','localtime') as int) <=18 then '下午'
when cast(strftime('%H','now','localtime') as int) >18 and cast(strftime('%H','now','localtime') as int) <=23 then '晚上'
else '凌晨' end;