1. Aggregate functions:
The aggregate functions supported by SQLite are also supported in many other relational databases. Therefore, here we will just give a brief description of each aggregate function, rather than giving more examples. For All Aggregate functions,DistinctKeywords can be used as pre-attributes of function parameter fields to ignore all repeated field values during calculation, such as Count (distinct X ).
Function |
Description |
AVG (X) |
This function returns the average value of the parameter fields in the same group. For string and blob field values that cannot be converted to numeric values, such as 'hello', SQLite regards it as 0. The results of the AVG function are always float. The only exception is that all field values are null, and the result of this function is null. |
Count (X | *) |
The count (x) function returns the number of rows in the same group where the value of X is not equal to null. The count (*) function returns the number of data rows in the same group. |
Group_concat (X [, y]) |
This function returns a string that connects all non-null x values. The y parameter of this function serves as the separator between each X value. If this parameter is ignored during calling, the default separator "," will be used during connection ",". In addition, the connection sequence between strings is uncertain. |
Max (X) |
This function returns the maximum value of the X field in the same group. If all values of this field are null, this function also returns NULL. |
Min (X) |
This function returns the minimum value of the X field in the same group. If all values of this field are null, the function returns NULL. |
Sum (X) |
The The function returns the sum of the x Field Values in the same group. If all the field values are null, the function returns NULL. If all x fields are integer or null, this function returns an integer value. Otherwise Returns a floating point value. It should be pointed out that if all data values are integer, an "integer overflow" exception will be thrown once the result exceeds the upper limit. |
Total (X) |
This function is not a standard SQL statement and has the same functions as sum, but the calculation result is more reasonable than sum. For example, if all fields are null, the function returns 0.0, which is different from Sum. Then, this function always returns a floating point value. This function will never throw an exception. |
Ii. core functions:
The following functions are built-in functions provided by SQLite by default. The descriptions and descriptions are listed as follows:
Function |
Description |
ABS (X) |
This function returns the absolute value of the numeric parameter X. If X is null, null is returned. If X is a string that cannot be converted to a numeric value, 0 is returned. If the X value exceeds the integer upper limit, the system throws an "integer overflow" exception. |
Changes () |
This function returns the number of data rows affected by the most recent insert, update, and delete statements. You can also run the C/C ++ function sqlite3_changes () to get the same result. |
Coalesce (X, Y ,...) |
Returns the first non-null parameter in the function parameter. If all the parameters are null, null is returned. This function has at least two parameters. |
Ifnull (x, y) |
This function is equivalent to the coalesce () function of two parameters. That is, the first function parameter not null is returned. If both are null, null is returned. |
Length (X) |
If X is a string, the number of characters is returned. If it is a numerical value, the length of the string representation of this parameter is returned. If it is null, null is returned. |
Lower (X) |
Returns the lowercase form of the function parameter X. By default, this function can only be applied to ASCII characters. |
Ltrim (X [, y]) |
If no optional parameter Y is available, this function removes all space characters on the left side of parameter X. If the parameter Y exists, remove any characters in Y on the left side of X. Returns the removed string. |
Max (X, Y ,...) |
Returns the maximum value of a function parameter. If any of the input values is null, null is returned. |
Min (X, Y ,...) |
Returns the minimum value of a function parameter. If any of the input values is null, null is returned. |
Nullif (x, y) |
If the function parameters are the same, null is returned. Otherwise, the first parameter is returned. |
Random () |
Returns an integer pseudo-random number. |
Replace (x, y, z) |
Replace all substring y in string-type function parameter X with string Z, and return the replaced string. The source string x remains unchanged. |
Round (X [, y]) |
Returns the value of X rounded to y scale. If y does not exist, the default value is 0. |
Rtrim (X [, y]) |
If y is not available, this function removes all space characters on the Right of X. If the parameter Y exists, remove any characters that appear in Y on the right side of X. Returns the removed string. |
Substr (X, Y [, Z]) |
Returns the sub-string of function parameter X. The Z-length character is truncated from the Y-th (the first character in X is 1). If the Z parameter is ignored, take all characters after the y character. If the value of Z is negative, the ABS (z) character is truncated from the Y-th to the left. If the Y value is negative, the count starts from the end of the X string to the position of ABS (y. |
Total_changes () |
This function returns the total number of rows affected by the insert, update, and delete statements since the connection is opened. We can also use the C/C ++ interface function sqlite3_total_changes () to get the same result. |
Trim (X [, y]) |
If y is not available, this function removes all space characters on both sides of X. If the parameter Y exists, remove any characters on both sides of X that appear in Y. Returns the removed string. |
Upper (X) |
Returns the upper case of the function parameter X. By default, this function can only be applied to ASCII characters. |
Typeof (X) |
Returns a string representation of the Data Type of function parameters, such as integer, text, real, and null. |
Iii. Date and time functions:
SQLite mainly supports the following four date-and time-related functions, such as:
1 ). date (timestring, modifier, modifier ,...)
2 ). time (timestring, modifier, modifier ,...)
3 ). datetime (timestring, modifier, modifier ,...)
4 ). strfti Me (format, timestring, modifier, modifier ,...)
all the above four functions use a time string as the parameter, followed by 0 or more modifiers. The strftime () function also accepts a format string as its first parameter. Strftime () and C Runtime libraries have the same name function. For the other three functions, the default format of the date function is: "YYYY-MM-DD", the default format of the time function is: "HH: mm: SS", the default format of the datetime function is: "YYYY-MM-DD hh: mm: SS ".
1. format information of the strftime function:
Format |
Description |
% D |
Day of month: 00 |
% F |
Fractional seconds: Ss. Sss |
% H |
Hour: 00-24 |
% J |
Day of Year: 001-366 |
% J |
Julian day number |
% M |
Month: 01-12 |
% M |
Minute: 00-59 |
% S |
Seconds since 1970-01-01 |
% S |
Seconds: 00-59 |
% W |
Day of week 0-6 with Sunday = 0 |
% W |
Week of Year: 00-53 |
% Y |
Year: 0000-9999 |
% |
% |
Note that the other three time functions can be expressed using strftime, for example:
Date (...) strftime ('% Y-% m-% d ',...)
Time (...) strftime ('% H: % m: % s ',...)
Datetime (...) strftime ('% Y-% m-% d % H: % m: % s ',...)
2. Time string format:
See the following list:
1). YYYY-MM-DD
2). YYYY-MM-DD hh: mm
3). YYYY-MM-DD hh: mm: SS
4). YYYY-MM-DD hh: mm: Ss. Sss
5). hh: mm
6). hh: mm: SS
7). hh: mm: Ss. Sss
8). Now
5) To 7) Only contains the time section. SQLite assumes that the date is 2000-01-01 . 8) The current time.
3. modifier:
See the following list:
1). NNN days
2). NNN hours
3). NNN minutes
4). NNN. NNNN seconds
5). NNN months
6). NNN years
7). Start of month
8). Start of year
9). Start of day
10). weekday n
1) To 6) You can simply add or subtract a specified number of date or time values. If the NNN value is negative, it is subtracted. Otherwise, it is added. 7) To 9) Set the specified date in the time string to the start of the current month, year, or day. 10) Forward the date to N of the next week, where Sunday is 0. Note: The order of modifier is extremely important. SQLite will execute the modifier in sequence from left to right.
4. Example:
-- Returns the current date.
SQLite> select date ('now ');
2012-01-15
-- Returns the last day of the current month.
SQLite> select date ('now ', 'start of month', '1 month','-1 Day ');
2012-01-31
-- Returns the number of seconds from 00:00:00 to the current time.
SQLite> select strftime ('% s', 'Now ');
1326641166
-- Returns the date of the first Tuesday in July of the current year.
SQLite> select date ('now ', 'start of year',' + 9 months', 'weekday 2 ');
2012-10-02