SQL truncation string functions

Source: Internet
Author: User
SUBSTRING (expression, start, length) returns a part of the character, binary, text, or image expression. The expression parameter is a string, binary string, text, image, column, or an expression that contains columns. Do not use expressions that contain aggregate functions. Start is an integer that indicates the start position of the substring.

SUBSTRING (expression, start, length) returns a part of the character, binary, text, or image expression. The expression parameter is a string, binary string, text, image, column, or an expression that contains columns. Do not use expressions that contain aggregate functions. Start is an integer that indicates the start position of the substring.

Substring (expression, start, length) returns a part of the character, binary, text, or image expression.
Parameters
Expression
Is a string, binary string, text, image, column, or expression that contains a column. Do not use expressions that contain aggregate functions.
Start
It is an integer that refers to the starting position of the stator string.
Length
It is an integer that refers to the length of the substring (the number of characters to return or the number of bytes ).

Use pubs
Select pub_id, substring (logo, 1, 10) as logo,
Substring (pr_info, 1, 10) as pr_info
From pub_info
Where pub_id = '20140901'

If exists (select table_name from information_schema.tables
Where table_name = 'npub _ info ')
Drop table npub_info
Go

View instances

Method 2: (similar to method 1)

Declare @ s1 varchar (100)
Select @ s1 = 'HTTP: // www.111cn.net'
Select substring (@ s1, patindex ('% www %', @ s1) + 1, len (@ s1 ))
-- This can also be written as follows: select substring (@ s1, patindex ('% // %', @ s1) + 2, len (@ s1 ))


Left (character_expression, integer_expression) returns the specified number of characters starting from the left of the string.
Parameters
Character_expression
Character or binary data expression. Character_expression can be a constant, variable, or column. Character_expression must be a data type that can be implicitly converted to varchar. Otherwise, use the cast function to digitally convert character_expression.
Integer_expression
Is a positive integer. If integer_expression is negative, an empty string is returned.
Return type
Varchar


Right (character_expression, integer_expression) returns the number of integer_expression characters specified from the right.
Parameters
Character_expression
An expression composed of character data. Character_expression can be a constant, variable, or a column of character or binary data.
Integer_expression
Is the starting position, expressed in a positive integer. If integer_expression is negative, an error is returned.
Return type
Varchar


Next let's take a look at

Mysql string truncation functions: left (), right (), substring (), substring_index (). There are also mid () and substr (). Here, mid () and substr () are equivalent to substring () functions. substring () functions are very powerful and flexible.
1. String truncation: left (str, length)
Mysql> select left ('sqlstudy. com', 3 );
+ ------------------------- +
| Left ('sqlstudy. com', 3) |
+ ------------------------- +
| SQL |
+ ------------------------- +
2. String truncation: right (str, length)
Mysql> select right ('sqlstudy. com', 3 );

Select reverse (left (reverse ('192. 123.222.abcdef '), charindex ('. ', reverse ('192. 123.222.abcdef'), 1)-1 ))

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.