當前位置:首頁 » 編程語言 » sql從左截取字元
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql從左截取字元

發布時間: 2022-07-12 04:35:27

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 取指定字元左邊的值

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 截取字元串

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截取語句 從左側開始截取到固定欄位

select substring(strname,0,len(strname)-len(5F357739-D41A-4585-BA8E-5391E9248572.wav) )as strname from clip

len(5F357739-D41A-4585-BA8E-5391E9248572.wav) 這個可以自己數一下長度 改為數字40
如:select substring(strname,0,len(strname)-40) as strname from clip

Ⅳ sql中where截取字元串字元

不管哪個庫,sql中一般都支持substring 或者它的變種 substr left right mid等

Ⅵ sql server 怎麼截取字元串

substring(要截取的字元串,從第幾位開始,截取幾位) 可以從任意位置截取
left(字元串,截取幾位) 從左側截取
right(字元串,截取幾位)從右側截取幾位

Ⅶ sql 截取字元串

簡單的:
select substr(『04.06.02.063-48-Z952』,1,instr(『04.06.02.063-48-Z952』,'-',1,2)-1) from al ;
instr(04.06.02.063-48-Z952,'-',1,2)的意思是找到第二個出現的-號,然後減去1,把左邊都截取下來就ok了。
實際使用的時候,用欄位名替換那個字元串就ok了。

希望幫到你,請採納最佳。

Ⅷ SQL中如何截取某列左邊四位字元開成新列

那就按照下列做法就OK
首先,需要對表追加一列(追加的varchar2類型的)
alter
table
table
add
AA
varchar2(4);
然後,往AA中寫入數據
update
table
table
set
AA=substr(A,1,4);
commit;
這樣就OK了,有問題繼續追問~
追加的列需要跟A列相同類型的話,把A列類型也告訴一下..

Ⅸ sql中如何在where字句里截取某個欄位的前幾位字元

sql中在where字句里截取字元方法如下:

1、如果是sqlserver:where left(p.end_time,4) = '2012'。

2、如果是Oracle:where substr(p.end_time,0,4) = '2012'。

舉例:

1、oracle: 'where substr(欄位名,1,2)='''123''''

2、sqlserver: 'where substring(欄位名,1,2)='''123''''

(9)sql從左截取字元擴展閱讀:

sql中,常用函數介紹:

1、AVG():返回平均值

2、COUNT():返回行數

3、FIRST():返回第一個記錄的值

4、LAST():返回最後一個記錄的值

5、MAX():返回最大值

6、MIN():返回最小值

7、SUM():返回總和

8、UCASE():將某個欄位轉換為大寫

9、LCASE():將某個欄位轉換為小寫

10、MID():從某個文本欄位提取字元

11、LEN():返回某個文本欄位的長度

12、ROUND():對某個數值欄位進行指定小數位數的四捨五入

13、NOW():返回當前的系統日期和時間

14、FORMAT():格式化某個欄位的顯示方式

15、INSTR():返回在某個文本域中指定字元的數值位置

16、LEFT():返回某個被請求的文本域的左側部分

17、RIGHT():返回某個被請求的文本域的右側部分

Ⅹ 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為截取的長度。