当前位置:首页 » 编程语言 » sql截取左边字符串
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql截取左边字符串

发布时间: 2022-11-13 04:21:57

sql数据库截取字符串函数

SQL截取字符串函数:
A.截取从字符串左边开始N个字符
以下是代码片段:
Declare
@S1
varchar(100)
Select
@S1='http://www.xrss.cn'
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.xrss.cn'
Select
SUBSTRING(@S1,8,3)
------------------------------------
显示结果:
www
以上例子皆是已知截取位置及长度,下面介绍未知位置的例子
2.截取未知位置的函数
A.截取指定字符串后的字符串(例如截取http://后面的字符串)
方法一:
以下是代码片段:
Declare
@S1
varchar(100)
Select
@S1='http://www.xrss.cn'
Select
Substring(@S1,CHARINDEX('www',@S1)+1,Len(@S1))
------------------------------------
显示结果:
www.163.com
需要注意:CHARINDEX函数搜索字符串时,不区分大小写,因此CHARINDEX('www',@S1)也可以写成CHARINDEX('WWW',@S1)
方法二:(与方法一类似)
以下是代码片段:
Declare
@S1
varchar(100)
Select
@S1='http://www.xrss.cn'
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.xrss.cn'
Select
REPLACE(@S1,'http://','')
------------------------------------
显示结果:
www.163.com
利用字符替换函数REPLACE,将除需要显示字符串外的字符替换为空
方法四:
以下是代码片段:
Declare
@S1
varchar(100)
Select
@S1='http://www.xrss.cn'
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获取需要截取的字符串长度

㈡ sql字符串截取查询,该怎么解决

SQL
Server
中截取字符串常用的函数:
1.LEFT
(
character_
expression
,
integer_expression
)
函数说明:LEFT
(
'源字符串'
,
'要截取最左边的字符数'
)
返回从字符串左边开始指定个数的字符
select
LEFT('SQL_Server_2008',4
);
返回结果:SQL_
2.RIGHT
(
character_expression
,
integer_expression
)
函数说明:RIGHT
(
'源字符串'
,
'要截取最右边的字符数'
)
返回字符串中从右边开始指定个数的
integer_expression
字符
select
RIGHT('SQL_Server_2008',4
);
返回结果:2008
3.
SUBSTRING
(
character_expression
,
start
,
length
)
函数说明:SUBSTRING
(
'源字符串'
,
'截取起始位置(含该位置上的字符)'
,
'截取长度'
)
返回字符、binary、text

image
表达式的一部分
select
SUBSTRING('SQL_Server_2008',5
,6);
返回结果:Server

㈢ sql数据库截取字符串函数

Oracle中 其语法为:
substr函数的用法,取得字符串中指定起始位置和长度的字符串 ,默认是从起始位置到结束的子串。

substr( string, start_position, [ length ] ) substr('目标字符串',开始位置,长度)
如:
substr('This is a test', 6, 2) would return 'is'
substr('This is a test', 6) would return 'is a test'
substr('TechOnTheNet', -3, 3) would return 'Net'
substr('TechOnTheNet', -6, 3) would return 'The'select substr('Thisisatest', -4, 2) value from al

㈣ SQL 截取字符串

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();
}。

㈤ 简单SQL语句,如何截取指定分隔符前字符串

一、用到的函数:substring(' ', , )、charindex(' ',' ')
select SUBSTRING('[email protected]',1,charindex('@','[email protected]')-1)
1.substring(字符串表达式,开始位置,长度):
从一个指定字符串的指定位置截取制定长度的字符;
第一个参数表示被截取的字符串;
第二个参数表示要在第一个参数中开始截取的位置;
第三个参数表示要截取的长度。
例如:select substring('abc123',1,2) →返回ab
从字符串‘abc123’的第一个字符开始截取,共截取两个字符,最后得到‘ab’。
2.charindex(字符串表达式1,字符串表达式2[,整数表达式]):
在字符串2中查找字符串1,如果存在返回第一个匹配的位置,如果不存在返回0。如果字符串1和字符串2中有一个是null则返回null。
可以指定在字符串2中查找的起始位置。
例如:select charindex('ab','BCabTabD') → 返回3
select charindex('ab','BCabTabD',4) →返回6

二、用到的函数:left(' ', )、charindex(' ',' ')
select LEFT('[email protected]',charindex('@','[email protected]')-1)
1.left(字符串表达式,整数表达式):
从字符串表达式的最左边开始截取整数表达式个字符。
例如:select left('abcdef',3) →返回abc

㈥ SQL中,取字符串从右边第2个字符到左边的所有字符,求语句

SQL中,取字符串从右边第2个字符到左边的所有字符可以参考下面的代码:

oracle

select substr('字符串',0,Len('字符串')-2) from al;

Sql Server

select substr('字符串',0,Len('字符串')-2)

(6)sql截取左边字符串扩展阅读:

sql语句

更新:update table1 set field1=value1 where 范围

查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)

排序:select * from table1 order by field1,field2 [desc]

求和:select sum(field1) as sumvalue from table1

㈦ sql 取指定字符左边的值

string mm = "2323%";
string nn = CommonClass.GetSubString(mm,"","%");

CommonClass 类是个公共方法
/// <summary>
/// 截取字符串
/// </summary>
/// <returns></returns>
public static string GetSubString(string Source, string strStart, string strEnd)
{
return GetSubString(Source, strStart, strEnd, 0);
}
/// <summary>
/// 截取字符串
/// </summary>
/// <returns></returns>
public static string GetSubString(string Source, string strStart, string strEnd, int startIndex)
{
string result = "";
int intStart = -1;
while (startIndex > 0)
{
Source = Source.Substring(Source.IndexOf(strStart) + strStart.Length);
startIndex--;
}
intStart = Source.IndexOf(strStart, startIndex);
if (intStart >= 0)
{
int intEnd = 0;
if (strEnd != "")
{
intEnd = Source.IndexOf(strEnd, intStart + strStart.Length);
}
else
{
intEnd = Source.Length;
}
if (intStart >= 0 && intEnd >= 0 && intStart + strStart.Length <= intEnd)
{
result = Source.Substring(intStart + strStart.Length, intEnd - intStart - strStart.Length);
}
}
return result;
}

㈧ sql如何截取字符

sql截取字符串:

1、LOCATE(substr, str):返回子串 substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,则返回0;

2、POSITION(substr IN str):返回子串 substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,与LOCATE函数作用相同;

3、LEFT(str, length):从左边开始截取str,length是截取的长度;

4、RIGHT(str,length):从右边开始截取str,length是截取的长度;

5、SUBSTRING_INDEX(str ,substr ,n):返回字符substr在str中第n次出现位置之前的字符串;

6、SUBSTRING(str,n ,m):返回字符串str从第n个字符截取到第m个字符;

7、REPLACE(str, n, m):将字符串str中的n字符替换成m字符;

8、LENGTH(str):计算字符串str的长度。

㈨ sql怎么样截取查询出来的字符串

SELECT
left(name,
CHARINDEX
(',',NAME)-1)
FROM

逗号前就是逗号左边了、
left函数

取指定字段某位置左边的字符串、
CHARINDEX(',',NAME)-1指定某字段中逗号开始的位置,-1是去除‘,’本身的位置
MYSQL
select
substring
_index(name,',',1)
from

截取字段值里第一个逗号左边的全部字符串

㈩ mssql数据库截取字符

mssql中截取字符串可以用left,right,substring函数。

left,是从字符左边开始截取,如:截取abcdefg字符串中的前三个字符:

selectleft('abcdefg',3);

其中3为截取的长度。

rigth是从字符右边开始截取,如截取abcdefg字符串中的后三个字符:

selectright('abcdefg',3);

其中3为截取的长度。

substring,是从任意位置截取,如截取abcdefg字符串中的第二到第四个字符:

selectsubstring('abcdefg',2,3);

其中2为开始截取的位数,3为截取的长度。