Outline
This article describes the function in SQLite, understand how to use the built-in functions to process data. The article is structured as follows:
- Introduction aggregate function
- Other built-in functions Introduction
- Time and date functions Introduction
- Comparison and processing of the data type is NULL values
First, aggregate functions
SQLite conventional polymerization function table below, for all aggregate functions, the front keyword attribute DISTINCT as a function of their parameter fields, field values to filter duplicate:
Function name | description |
---|---|
count(x | *) | Non-NULL field number of rows x, count (*) returns the number of rows in the same group |
avg (x) | The average value of x calculated field |
max(x) | Select the maximum value of the field of x |
min (x) | Selecting a minimum value of x Field |
sum(x) | Calculates the sum of a numeric column |
total(x) | This function does not belong to the SQL standard, which is substantially the same function and the sum, but the result is more reasonable than the calculated sum. For example, when all the field values are NULL, the function returns 0.0. Then there is the function always returns a floating-point value. This function does not always throw an exception. |
group_concat(x [,y]) | This function returns a string that will be connected to all non-NULL values of x. y parameters of the function as a separator between each x value, if the parameter is ignored when the call is connected when the default delimiter "," |
We should particularly note count function, for example, Contacts data table is as follows:
sqlite> SELECT * FROM Contacts;
id name phone
---------- ---------- ----------
1 Marck NULL
2 Beryl 10086
3 Micheal 10010
4 Tom NULL
Can be seen in Table 4, a total of the data, which has two phone field is empty. When we use the count (*), returns the number of rows in the table:
sqlite> SELECT count(*) FROM Contacts;
count(*)
----------
4
Note that, even if the data of a row are all NULL (table permitting), then count (*) still count the rows of data.
When the parameter is a field count table function returns non-NULL rows of the field:
sqlite> SELECT count(phone) FROM Contacts;
count(phone)
----------
2
Second, other functions
In addition to the aforementioned aggregation function, SQLite also provides the following functions:
Function name | description |
---|---|
abs(x) | Returns the absolute value of the parameter x |
length(x) | If the argument X is a string, returns the number of characters, if the value of the parameter indicates the length of the string is returned in the form of, if it is NULL, then return NULL |
upper(x) | Returns the uppercase parameter x, the function applies only ASCII characters |
lower(x) | Returns the lowercase parameter x, the function applies only ASCII characters |
random() | Returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807 |
ltrim (x [, and]) | If there is no optional parameters Y, the function removes all spaces left of the parameter X; if parameter Y, X left any character appearing in Y is removed. Back to the removal of the last string |
rtrim(x [,y]) | If there is no optional parameters Y, the function removes all spaces to the right of the parameter X; if parameter Y, X to the right of any character appearing in Y is removed. Back to the removal of the last string |
trim(x [,y]) | If there is no optional parameters Y, the function removes all spaces on both sides of the X parameters. If there is argument Y, X on both sides of any character appearing in Y is removed. Back to the removal of the last string |
changes() | Return INSERT, UPDATE, and DELETE statements are affected by the recent execution of data rows |
coalesce(x,y,…) | Function returns the first non-NULL argument parameter, if the parameter is NULL, return NULL |
ifnull(x,y) | Returns a function parameter is not NULL, and if both are NULL, NULL is returned |
typeod(x) | Returns the type of function parameter data string representation, such as "Integer, text, real, null", etc. |
nullif(x,y) | If the x, y equal returns NULL, otherwise it returns the first argument x |
Third, the time and date functions
SQLite supports the following five date and time functions:
Function name | description |
---|---|
date(timestring, modifier, modifier, …) | Return dates to YYYY-MM-DD format |
time(timestring, modifier, modifier, …) | MM:: SS format returns the time as HH |
datetime(timestring, modifier, modifier, …) | 以 YYYY-MM-DD HH:MM:SS 格式返回 |
julianday(timestring, modifier, modifier, …) | 返回从格林尼治时间的公元前 4714 年 11 月 24 日正午算起的天数 |
strftime(format, timestring, modifier, modifier, …) | 根据第一个参数指定的格式字符串返回格式化的日期 |
上述五个日期和时间函数把时间字符串作为参数。时间字符串后跟零个或多个 modifier 修饰符。strftime() 函数也可以把格式字符串 format 作为其第一个参数。
1. 时间字符串(timeString)
时间字符串可以采用下列的任意格式:
时间字符串 | 实例 |
---|---|
YYYY-MM-DD | 2019-04-20 |
YYYY-MM-DD HH:MM | 2019-04-20 22:43 |
YYYY-MM-DD HH:MM:SS | 2019-04-20 22:43:46 |
YYYYMMDD HHMMSS | 20190420 224346 |
YYYY-MM-DDTHH:MM | 2019-04-20 22:43:46 |
HH:MM:SS | 22:43:46 |
now | 2019-04-20 |
2. 修饰符(modifier)
修饰符 | 描述 |
---|---|
NNN days | 用于天数的调整,+1 day表示向后一天,-1 day表示向前一天 |
NNN hours | 用于小时的调整,+1 hour表示向后一小时,-1 hour表示向前一小时 |
NNN minutes | 用于分钟的调整,+1 minute表示向后一分钟,-1 minute表示向前一分钟 |
NNN.NNNN seconds | 用于秒的调整,+1 second表示向后一秒,-1 second表示向前一秒 |
NNN months | 用于月份的调整,+1 month表示向后一个月,-1 month表示向前一个月 |
NNN years | 用于年份的调整,+1 year表示向后一年,-1 year表示向前一年 |
start of month | 某一日期的当前月份 |
start of year | 某一日期的当前年份 |
start of day | 某一日期的当前天数 |
weekday N | 表示星期N,N取值为0~6,分别表示周日 ~ 周六 |
unixepoch | UNIX 时间戳 |
localtime | 本地时区的日期和时间 |
utc | 转化为UTC时间值 |
3. 格式化(format)
格式化是用于 strftime 函数的参数,可采用值如下:
格式 | 描述 |
---|---|
%d | 一月中的第几天,01-31 |
%f | 带小数部分的秒,SS.SSS |
%H | 小时,00-23 |
%j | 一年中的第几天,001-366 |
%J | 儒略日数,DDDD.DDDD |
%m | 月,00-12 |
%M | 分,00-59 |
%s | 从 1970-01-01 算起的秒数 |
%S | 秒,00-59 |
%w | 一周中的第几天,0-6 (0 is Sunday) |
%W | 一年中的第几周,01-53 |
%Y | 年,YYYY |
%% | % symbol |
4. 实例
计算当前月份的最后一天:
sqlite> SELECT date('now','start of month', '+1 month', '-1 day') AS last_day_of_this_month;
last_day_of_this_month
----------------------
2019-04-30
计算当年 10 月的第一个星期二的日期:
sqlite> SELECT date('now', 'start of year', '+9 month', 'weekday 2') AS october_first_tues;
october_first_tues
------------------
2019-10-01
计算从 2004 年某一特定时刻以来的秒数:
sqlite> SELECT strftime('%s', 'now')-strftime('%s', '2004-01-01 11:26:00') AS Count;
Count
----------
482817426
计算新中国成立以来至今的天数:
sqlite> SELECT julianday('now')-julianday('1949-10-01') AS China_Found_Day;
China_Found_Day
----------------
25403.6443211804
当前时间与UTC时间的转换:
sqlite> SELECT datetime('now', 'utc') AS UTC;
UTC
-------------------
2019-04-20 07:24:24
四、数据类型的比较和NULL值的处理
首先创建一个表 Test,然后插入任意的值,如下所示:
CREATE TABLE Test(whatever);
INSERT INTO Test VALUES('314159');
INSERT INTO Test VALUES(314159);
INSERT INTO Test VALUES(x'314159');
INSERT INTO Test VALUES(3.14159);
INSERT INTO Test VALUES(NULL);
我们在插入这些值之后,通过 SELECT 和 typeof 函数来查看我们插入的值:
SELECT whatever, typeof(whatever)
FROM Test;
whatever typeof(whatever)
---------- ----------------
314159 text
314159 integer
1AY blob
3.14159 real
NULL null
可以看到字段 whatever 没有添加任何约束,因此它可以存储不同的类。我们通过 typeof 函数也验证了它确实存储了不同的类,那这些不同的类之间是如何比较大小的呢?事实上它们遵循一定的规则:
- NULL 存储类聚有最低的类值。一个具有 NULL 存储类的值比所有其他的值都小(包括其它具有 NULL 存储类的值)。在 NULL 值之间,没有具体的排序顺序。
- integer 或 real 存储类值高于 NULL,它们的类值相等,integer 值和 real 值通过去数值进行比较。
- text 存储类的值比 integer 和 real 高。数值永远比字符串的值低。当两个 text 值进行比较时,其大小由其值中定义的“排序法”决定。
- blob 存储类具有最高的类值。具有 blob 类的值大于其他所有类的值。
所以当我们执行下列语句时,我们就能知道会得出什么结果了:
SELECT whatever, typeof(whatever)
FROM Test
ORDER BY whatever;
whatever typeof(whatever)
---------- ----------------
NULL null
3.14159 real
314159 integer
314159 text
1AY blob
The results are consistent with what we call the rule. After learning a comparison between data types, database Next we see more special NULL value.
NULL value
NULL values expressed in support for SQLite in an "unknown" or the concept of "agnostic" is. NULL is a placeholder for missing information, not the value itself. NULL indicates that the location has no value: NULL is not other values, NULL is not true, nor false, not zero, nor is it an empty string. In SQLite, the NULL on the following three conventions:
- In order to use the logic expressions NULL, SQLite tristate logic used, NULL is one truth value. The following table shows the relationship between the AND, OR and NULL:
x | Y | x AND y | x OR y |
---|---|---|---|
True | True | True | True |
True | False | False | True |
True | NULL | NULL | True |
False | False | False | False |
False | NULL | False | NULL |
NULL | NULL | NULL | NULL |
- It can be NULL or IS NOT NULL NULL detecting presence or absence of the operator by IS.
- NULL is not equal to other values, including NULL, NULL can not be compared with other values, will not NULL NULL value greater or less than another.
reference
SQLite study manual (built-in function)
"SQLite Definitive Guide"
- Chapter 3 sqlite in sql
- Chapter 4 sqlite Advanced sql
I hope this article to help you ~