SQL Intercept String function

Source: Internet
Author: User
Tags mysql tutorial

SUBSTRING (expression, start, length) returns part of a character, binary, text, or an image expression.
Parameters
Expression
is a string, binary string, text, image, column, or an expression that contains a column. Do not use an expression that contains aggregate functions.
Start
Is an integer that specifies the starting position of the substring.
Length
Is an integer that specifies the length of the substring (the number of characters or bytes to be returned).

Use
pubs
Select pub_id, substring (logo, 1,) as logo,
SUBSTRING (pr_info, 1,) as Pr_info
From pub_info
where pub_id = ' 1756 '

If exists (select table_name from Information_schema.tables
WHERE table_name = ' npub_info ')
drop table Npub_info
Go

Look at an example

Method Two: (similar to the method one)

declare @s1 varchar (100)
Select @s1 = ' Http://www.111cn.net '
Select substring (@s1, patindex ('%www% ', @s1) +1,len (@s1))
It can also be written here: Select substring (@s1, patindex ('%//% ', @s1) +2,len (@s1))


Left (character_expression, integer_expression) returns a specified number of characters starting from the left-hand side of the string.
Parameters
character_expression
A character or binary data expression. Character_expression can be constants, variables, or columns. Character_expression must be a data type that can be implicitly converted to varchar. Otherwise, use the CAST function to explicitly convert the 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 integer_expression character in the string that specifies the number of occurrences from the right-hand side.
Parameters
character_expression
An expression that consists of character data. Character_expression can be constants, variables, or columns of character or binary data.
Integer_expression
Is the starting position, represented by a positive integer. If integer_expression is a negative number, an error is returned.
return type
varchar


Here's a look at the MySQL tutorial

MySQL string intercept function: Left (), right (), substring (), Substring_index (). There is also mid (), substr (). where mid (), substr () is equivalent to the substring () function, substring () is very powerful and flexible.
1. String interception: Left (str, length)
Mysql> Select Left (' sqlstudy.com ', 3);
+-------------------------+
| Left (' sqlstudy.com ', 3) |
+-------------------------+
| SQL |
+-------------------------+
2. String interception: Right (str, length)
Mysql> Select Right (' sqlstudy.com ', 3);

Select reverse (reverse (' 123.123.222.abcdef '), charindex ('. ', reverse (' 123.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.