MySQL string function, date time function

Source: Internet
Author: User
Tags rtrim strcmp string format

First, common String functions:

    • 1. Char_length Gets the length (in characters)
    • 2. Format formatted
    • 3, insert replace the way inserted
    • 4. INSTR Get Location
    • 5, Left/right take left, take right
    • 6. Length Gets the lengths (in bytes)
    • 7. Ltrim/rtrim/trim to go space (left/right/Custom)
    • 8. STRCMP string comparison
    • 9. CONCAT string concatenation
    • 10. SUBSTRING string interception

1.char_length: Gets the length (in characters)

Char_length (str)

Explanation: The return value is the length of the string str, the calculated unit is a character (a Chinese is also counted as a character)

Example:

If you are querying the length of a field in a known table, you can write:

2.FORMAT:

FORMAT (X,D)

Explanation: Writes the format of the number x as ' #,###,###.## ', preserves the D-bit after the decimal point in a rounded manner, and returns the result as a string. If D is 0, the result is returned without a decimal point, or with no fractional part.

Example:

3, insert: Replace the way inserted

INSERT (str,pos,len,newstr)

Explanation: Returns the string str, starting at the POS location of the original STR, replaced with the string newstr, and the length of the replacement is Len.

If the POS exceeds the string length, the return value is the original string. If Len is longer than the length of the subsequent string, it is replaced with the end of the string from position pos.

If any one of the four parameters is null, the return value of the entire function is null.

Example: Replacing a smyh001 with a smyh002

4、INSTR:查找子字符串的位置

INSTR (str,substr)

Explanation: Returns the position of the substring substr in the string str for the first time.

Example:

5.left/right: string interception

    • Left Intercept:
Left (str,len)

Explanation: Returns the string str, starting from the left, the first Len characters.

    • Right intercept:
Right (str,len)

Explanation: Returns the string str, starting from the right, the first Len characters.

6, Length: The length of thestring ( in bytes)

LENGTH (str)

Explanation: The return value is the length of the string str, in bytes. One multibyte character is counted as multibyte. This means that for a string containing five 2-byte characters, the return value of LENGTH () is 10, and the return value of Char_length () is 5.

Note:

    • In GBK encoding, a Chinese character is 2 bytes.
    • In UTF-8 encoding, a Chinese character is 3 bytes, and an English character is 2 bytes.

7.Ltrim/rtrim/trim go to space (left/Right/Custom):

    • Remove the left space:
LTRIM (str)

Explanation: Returns the string str, whose boot space character is deleted.

    • Remove the middle space:
RTRIM (str)

Explanation: Returns the string str with the trailing space character being deleted.

    • Custom Go spaces:
TRIM ([{BOTH | Leading | TRAILING} [Remstr from]  

Note: The arguments inside the braces and brackets are optional, and if all are added, the default is parameter both.

Explanation: The original string str is stripped back with a space. Just go to the beginning (leading) of the space, only the end (TRAILNG) of the space, or at the same time remove (both) the beginning and end of the space line. If none of the classifier both, leadin, or trailing is given, it is assumed to be both.

TRIM ([remstr fromstr)

Explanation: The function of the REMSTR keyword is to remove the specified character. That is, all prefixes and/or suffixes that contain remstr will be deleted. If not specified, the space is removed.

Examples of official documents:

8.STRCMP: Determine if the strings are equal

STRCMP (EXPR1,EXPR2)

Explanation: Determines whether two strings are equal. Returns 0 if all strings are the same, or 1 if the first argument is less than the second, according to the current sorting order; other cases return 1. Functions and comparators are similar.

Note: The size of the string is compared by ANSI encoding.

Example:

9.CONCAT: string concatenation

CONCAT (STR1,STR2,...)

Explanation: The return result is str1, str2 The string that is generated after stitching. If any one of the arguments is NULL, the return value is null. There may be one or more parameters in parentheses.

If all parameters are non-binary strings, the result is a non-binary string. If the argument contains any binary string, the result is a binary string. A numeric parameter is automatically converted to the binary string format that is equal to it.

To avoid this situation where the binary is present, use explicit type cast (str) to convert the binary string to a non-binary string. Example: SELECT CONCAT (CAST (Int_col as CHAR), Char_col)

Example: (The Navicat editor automatically converts a binary string to a non-binary string)

10.SUBSTRING: string interception

SUBSTRING (str, POS)SUBSTRING (from POS)SUBSTRING (str,pos,len)SUBSTRING ( 

Explanation: The format without the Len parameter returns a substring from the string str, starting at position pos. The format with the Len parameter returns a substring of the same length as the Len character from the string str, starting at position pos.

Use the from format as standard SQL syntax.

You may also use a negative value for the POS. In this case, the position of the substring starts at the POS character at the end of the string (that is, the POS is positive, from the left, and the POS is negative, starting from the right).

Examples of official documents are as follows:

Second, the common date and time function:

    • 1, DAYOFWEEK, DayOfYear, DayOfMonth
    • 2, WEEKDAY
    • 3, year, QUARTER, MONTH,
    • 4, HOUR, MINUTE, SECOND
    • 5, to days, from days
    • 6, Curdate, Curtime, now get current time

1,DAYOFWEEK, DayOfYear, DayOfMonth

    • Days of the Week: (The day of the week)
DAYOFWEEK (date)

Explanation: Returns the weekday index for date (1= Sunday, 2 = Monday, ..., 7 = Saturday). Thatis, the range of date corresponds to 1 to 7.

    • Find the day of the Year: (Day ordinal of the year)
DayOfYear (date)

Returns the number of days in a year that corresponds to a date, ranging from 1 to 366.

    • The day of the one month: (Day of the one month)
DayOfMonth (date)

Returns the date corresponding to the month, ranging from 1 to 31. Note: Day (date) and DayOfMonth (date) have the same meaning.

Example:

2,WEEKDAY:

    • Days of the Week: (The day of the week)
WEEKDAY (date)

Explanation: Returns date (0 = Monday, 1 = Tuesday, ... 6 = Sunday) The corresponding working day index. Thatis, the range of date corresponds to 0 to 6.

Note: DAYOFWEEK (date) and weekday (date) have the same functionality, and the only difference is the range of values that the date corresponds to.

3, year , QUARTER, MONTH

    • Year of Evaluation:
Year (date)

Explanation: Returns the date corresponding to the year, ranging from 1000 to 9999.

    • Quarterly:
QUARTER (date)

Explanation: Returns the quarter value in the year corresponding to date, ranging from 1 to 4.

    • Month: (The month is the first month of the year)
MONTH (date) 

Explanation: Returns the month of date, ranging from 1 to 12.

Example:

4,HOUR, MINUTE, SECOND

    • Ask for hours:
HOUR (Time)

Explanation: Returns the number of hours corresponding to time. The return value range for the day value is from 0 to 23.

    • Ask for minutes:
MINUTE (Time)

Explanation: Returns the time corresponding to the number of minutes, ranging from 0 to 59.

    • To find the number of seconds:
SECOND (Time)

Explanation: Returns the time corresponding to the number of seconds, ranging from 0 to 59.

Example:

5,to_days, From_days

    • To ask for a date, a total of many days: (in history)
To_days (date)

Explanation: Given a day date, returns a number of days, starting from year 0.

    • The date of the first day of the history of the river:
From_days (N)

Explanation: Given a number of days, N, returns a date value.

When using From_days () to process an ancient date, it is important to be cautious that he is not used to deal with the number of days before the Gregorian calendar (1582).

6,curdate, Curtime, now

    • For the current month and day:
Curdate ()

Explanation: Returns the current date as a value in the ' YYYY-MM-DD ' or YYYYMMDD format, depending on whether the function is used in a string or in a digital context.

Example:

    • To find the current time division seconds:
Curtime ()

Explanation: Returns the current time in the format ' HH:MM:SS ' or HHMMSS, depending on whether the function is used in a string or in a digital context.

Example:

    • Ask for the current month day, time division seconds:
Now ()

Example:

Note: If now is placed in the statement, look at the format requirements for the specific field. For example, if it is placed in the Date field, only the month day is displayed, and if it is placed in the DateTime field, the date and time of day and seconds are displayed.

MySQL string function, date time function

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.