字符串截取函数,只限单字节字符使用(对于中文的截取时遇上奇数长度是会出现乱码,需另行处理),本函数可截取字符串指定范围内的字符。
SUBSTRING返回字符、binary、text 或 image 表达式的一部分。有关可与该函数一起使用的有效 Microsoft® SQL Server™ 数据类型的更多信息,请参见数据类型。
语法
SUBSTRING ( expression , start , length )
参数
expression
是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。
start
是一个整数,指定子串的开始位置。
length
是一个整数,指定子串的长度(要返回的字符数或字节数)。
substring()
——任意位置取子串
left()
right()
——左右两端取子串
ltrim()
rtrim()
——截断空格,没有trim()。
charindex()
patindex()
——查子串在母串中的位置,没有返回0。区别:patindex支持通配符,charindex不支持。
函数功效:
字符串截取函数,只限单字节字符使用(对于中文的截取时遇上奇数长度是会出现乱码,需另行处理),本函数可截取字符串指定范围内的字符。
应用范围:
标题、内容截取
函数格式:
string substr ( string string, int start [, int length])
参数1:处理字符串
参数2:截取的起始位置(第一个字符是从0开始)
参数3:截取的字符数量
substr()更多介绍可在PHP官方手册中查询(字符串处理函数库)
举例:
substr("ABCDEFG", 0); //返回:ABCDEFG,截取所有字符
substr("ABCDEFG", 2); //返回:CDEFG,截取从C开始之后所有字符
substr("ABCDEFG", 0, 3); //返回:ABC,截取从A开始3个字符
substr("ABCDEFG", 0, 100); //返回:ABCDEFG,100虽然超出预处理的字符串最长度,但不会影响返回结果,系统按预处理字符串最大数量返回。
substr("ABCDEFG", 0, -3); //返回:EFG,注意参数-3,为负值时表示从尾部开始算起,字符串排列位置不变
例子:
1.截取已知长度的函数
A.截取从字符串左边开始N个字符
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select Left(@S1,4)
------------------------------------
显示结果: http
B.截取从字符串右边开始N个字符(例如取字符 www.163.com )
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select right(@S1,11)
------------------------------------
显示结果: www.163.com
C.截取字符串中任意位置及长度(例如取字符www)
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select SUBSTRING(@S1,8,3)
------------------------------------
显示结果: www.163.com
以上例子皆是已知截取位置及长度,下面介绍未知位置的例子
2.截取未知位置的函数
A.截取指定字符串后的字符串(例如截取http://后面的字符串)
方法一:
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select Substring(@S1,CHARINDEX('www',@S1)+1,Len(@S1))
/*此处也可以这样写:Select Substring(@S1,CHARINDEX('//',@S1)+2,Len(@S1))*/
------------------------------------
显示结果: www.163.com
需要注意:CHARINDEX函数搜索字符串时,不区分大小写,因此CHARINDEX('www',@S1)也可以写成CHARINDEX('WWW',@S1)
方法二:(与方法一类似)
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select Substring(@S1,PATINDEX('%www%',@S1)+1,Len(@S1))
--此处也可以这样写:Select Substring(@S1,PATINDEX('%//%',@S1)+2,Len(@S1))
------------------------------------
显示结果: www.163.com
函数PATINDEX与CHARINDEX区别在于:前者可以参数一些参数,增加查询的功能
方法三:
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select REPLACE(@S1,'http://','')
------------------------------------
显示结果: www.163.com
利用字符替换函数REPLACE,将除需要显示字符串外的字符替换为空
方法四:
代码如下:
Declare @S1 varchar(100)
Select @S1='http://www.163.com'
Select STUFF(@S1,CHARINDEX('http://',@S1),Len('http://'),'')
------------------------------------
显示结果: www.163.com
函数STUFF与REPLACE区别在于:前者可以指定替换范围,而后者则是全部范围内替换
B.截取指定字符后的字符串(例如截取C:\Windows\test.txt中文件名)
与A不同的是,当搜索对象不是一个时,利用上面的方法只能搜索到第一个位置
方法一:
代码如下:
Declare @S1 varchar(100)
Select @S1='C:\Windows\test.txt'
select right(@S1,charindex('\',REVERSE(@S1))-1)
-------------------------------------
显示结果: text.txt
利用函数REVERSE获取需要截取的字符串长度
substr()
例子:
代码如下:
private void DDL_AreaBind()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
string str = "0000";
cmd = new SqlCommand("select AreaID,Name=ltrim(Name) from Area where right(AreaID,4) ='" + str + "'", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds, "area");
this.ddl_area.DataSource = ds.Tables["area"].DefaultView;
this.ddl_area.DataTextField = "Name";
this.ddl_area.DataValueField = "AreaID";
this.ddl_area.DataBind();
cmd = new SqlCommand("select * from Area ", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "city");
this.ddl_city.DataSource = ds.Tables["city"].DefaultView;
this.ddl_city.DataTextField = "Name";
this.ddl_city.DataValueField = "AreaID";
this.ddl_city.DataBind();
}
protected void ddl_area_SelectedIndexChanged(object sender, EventArgs e)
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
this.ddl_city.Enabled = true;
string str1="0000";
cmd = new SqlCommand("select AreaID,Name from Area where substring(AreaID,1,2)='" + this.ddl_area.SelectedValue.Substring(0,2) + "' AND substring(AreaID,3,4) '0000' AND substring(AreaID,5,2)='00' ", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "city");
this.ddl_city.DataSource = ds.Tables["city"].DefaultView;
this.ddl_city.DataTextField = "Name";
this.ddl_city.DataValueField = "AreaID";
this.ddl_city.DataBind();
}

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.