SQLite Quick Start-function usage

Source: Internet
Author: User
Tags julian day

Original Function address
First, let's give a few simple examples.

1. Return the current date

2. Return the last day of the month.

3,Returns the number of seconds that have elapsed since 00:00:00 to the current time.

 

4. Return the date of the first Monday of the current month.

 

 

I. Date and Time Functions: original address

SQLite mainly supports the following four date-and time-related functions, such:
1 ).Date(Timestring, modifier, modifier ,...)
2 ).Time(Timestring, modifier, modifier ,...)
3 ).Datetime(Timestring, modifier, modifier ,...)
4 ).Strftime(Format, timestring, modifier, modifier ,...)
All the above four functions accept 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)To7)Only contains the time section. SQLite assumes that the date is2000-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)To6)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)To9)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.

Ii. Other functions

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) This function returns the sum of the x Field Values in the same group. If all the field values are null, this function also returns NULL. If all x Field Values are integer or null, this function returns an integer value. Otherwise, a floating point value is returned. It should be pointed out that if all data values are integer, an "integer" will be thrown once the result exceeds the upper limit.
Overflow "exception.
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.

 

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, an integer is thrown.
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.