{"id":343,"date":"2023-09-16T10:44:55","date_gmt":"2023-09-16T02:44:55","guid":{"rendered":""},"modified":"2023-09-16T15:50:11","modified_gmt":"2023-09-16T07:50:11","slug":"sql%e6%88%aa%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/mushiming.com\/343.html","title":{"rendered":"SQL\u622a\u53d6\u5b57\u7b26\u4e32"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

GPS\u5e73\u53f0\u3001\u7f51\u7ad9\u5efa\u8bbe\u3001\u8f6f\u4ef6\u5f00\u53d1\u3001\u7cfb\u7edf\u8fd0\u7ef4\uff0c\u627e\u68ee\u5927\u7f51\u7edc\u79d1\u6280\uff01
https:\/\/cnsendnet.taobao.com
\u6765\u81ea\u68ee\u5927\u79d1\u6280\u5b98\u65b9\u535a\u5ba2
http:\/\/www.cnsendblog.com\/index.php\/?p=2022<\/p>\n

SQL\u4f7f\u7528charindex\u548csubstring\u622a\u53d6\u5b57\u7b26\u4e32
SUBSTRING
\u8fd4\u56de\u5b57\u7b26\u3001binary\u3001text \u6216 image\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u3002\u6709\u5173\u53ef\u4e0e\u8be5\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u7684\u6709\u6548Microsoft SQL Server \u6570\u636e\u7c7b\u578b\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

\u8bed\u6cd5
SUBSTRING(expression,starting_position int,length int)<\/p>\n

\u53c2\u6570
expression
\u662f\u8981\u4ece\u54ea\u4e2a\u5b57\u7b26\u4e32\u4e2d\u622a\u53d6\u5c31\u5199\u90a3\u4e2a\u5b57\u7b26\u4e32\u3001\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3001text\u3001image\u3001\u5217\u6216\u5305\u542b\u5217\u7684\u8868\u8fbe\u5f0f\u3002\u4e0d\u8981\u4f7f\u7528\u5305\u542b\u805a\u5408\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n

starting_position
\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u6307\u5b9a\u5b50\u4e32\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002
\u6ce8\u610f\uff1a\u5f00\u59cb\u4f4d\u7f6e\u662f\u4ece1\u5f00\u59cb\uff0c\u5982abcdefg\u4e2dc\u662f\u7b2c3\u4e2a\u3002<\/p>\n

length
\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u6307\u5b9a\u5b50\u4e32\u7684\u957f\u5ea6\uff08\u8981\u8fd4\u56de\u7684\u5b57\u7b26\u6570\u6216\u5b57\u8282\u6570\uff09\u3002<\/p>\n

substring() \u2014\u2014\u4efb\u610f\u4f4d\u7f6e\u53d6\u5b50\u4e32<\/p>\n

left()
right()
\u2014\u2014\u5de6\u53f3\u4e24\u7aef\u53d6\u5b50\u4e32<\/p>\n

ltrim()
rtrim()
\u2014\u2014\u622a\u65ad\u7a7a\u683c\uff0c\u6ca1\u6709trim()\u3002<\/p>\n

charindex()
patindex()
\u2014\u2014\u67e5\u5b50\u4e32\u5728\u6bcd\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6ca1\u6709\u8fd4\u56de0\u3002\u533a\u522b\uff1apatindex\u652f\u6301\u901a\u914d\u7b26\uff0ccharindex\u4e0d\u652f\u6301\u3002
\u51fd\u6570\u529f\u6548\uff1a
\u5b57\u7b26\u4e32\u622a\u53d6\u51fd\u6570\uff0c\u53ea\u9650\u5355\u5b57\u8282\u5b57\u7b26\u4f7f\u7528\uff08\u5bf9\u4e8e\u4e2d\u6587\u7684\u622a\u53d6\u65f6\u9047\u4e0a\u5947\u6570\u957f\u5ea6\u662f\u4f1a\u51fa\u73b0\u4e71\u7801\uff0c\u9700\u53e6\u884c\u5904\u7406\uff09\uff0c\u672c\u51fd\u6570\u53ef\u622a\u53d6\u5b57\u7b26\u4e32\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\u3002<\/p>\n

\u5e94\u7528\u8303\u56f4\uff1a
\u6807\u9898\u3001\u5185\u5bb9\u622a\u53d6<\/p>\n

\u51fd\u6570\u683c\u5f0f\uff1a
string substr(string string, int start [, int length])
\u53c2\u65701\uff1a\u5904\u7406\u5b57\u7b26\u4e32
\u53c2\u65702\uff1a\u622a\u53d6\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u4ece0\u5f00\u59cb\uff09
\u53c2\u65703\uff1a\u622a\u53d6\u7684\u5b57\u7b26\u6570\u91cf
substr()\u66f4\u591a\u4ecb\u7ecd\u53ef\u5728PHP\u5b98\u65b9\u624b\u518c\u4e2d\u67e5\u8be2\uff08\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u5e93\uff09
1.\u622a\u53d6\u5df2\u77e5\u957f\u5ea6\u7684\u51fd\u6570<\/p>\n

A.\u622a\u53d6\u4ece\u5b57\u7b26\u4e32\u5de6\u8fb9\u5f00\u59cbN\u4e2a\u5b57\u7b26
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select Left(@S1,4)
------------------------------------
\u663e\u793a\u7ed3\u679c: http
B.\u622a\u53d6\u4ece\u5b57\u7b26\u4e32\u53f3\u8fb9\u5f00\u59cbN\u4e2a\u5b57\u7b26(\u4f8b\u5982\u53d6\u5b57\u7b26www.163.com)
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select right(@S1,11)
------------------------------------
\u663e\u793a\u7ed3\u679c: www.163.com<\/p>\n

C.\u622a\u53d6\u5b57\u7b26\u4e32\u4e2d\u4efb\u610f\u4f4d\u7f6e\u53ca\u957f\u5ea6(\u4f8b\u5982\u53d6\u5b57\u7b26www)
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select SUBSTRING(@S1,8,3)
------------------------------------
\u663e\u793a\u7ed3\u679c: www<\/p>\n

 \u4ee5\u4e0a\u4f8b\u5b50\u7686\u662f\u5df2\u77e5\u622a\u53d6\u4f4d\u7f6e\u53ca\u957f\u5ea6,\u4e0b\u9762\u4ecb\u7ecd\u672a\u77e5\u4f4d\u7f6e\u7684\u4f8b\u5b50\n<\/code><\/pre>\n

2.\u622a\u53d6\u672a\u77e5\u4f4d\u7f6e\u7684\u51fd\u6570
A.\u622a\u53d6\u6307\u5b9a\u5b57\u7b26\u4e32\u540e\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u622a\u53d6http:\/\/\u540e\u9762\u7684\u5b57\u7b26\u4e32)
\u65b9\u6cd5\u4e00:
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select Substring(@S1,CHARINDEX(\u2018www\u2019,@S1)+1,Len(@S1))
\/\u6b64\u5904\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199:Select Substring(@S1,CHARINDEX(\u2019\/\/\u2019,@S1)+2,Len(@S1))<\/em>\/
------------------------------------
\u663e\u793a\u7ed3\u679c: www.163.com<\/p>\n

\u9700\u8981\u6ce8\u610f:CHARINDEX\u51fd\u6570\u641c\u7d22\u5b57\u7b26\u4e32\u65f6,\u4e0d\u533a\u5206\u5927\u5c0f\u5199,\u56e0\u6b64CHARINDEX(\u2018www\u2019,@S1)\u4e5f\u53ef\u4ee5\u5199\u6210CHARINDEX(\u2018WWW\u2019,@S1)
\u65b9\u6cd5\u4e8c:(\u4e0e\u65b9\u6cd5\u4e00\u7c7b\u4f3c)
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select Substring(@S1,PATINDEX(\u2019%www%\u2019,@S1)+1,Len(@S1))
\u2013\u6b64\u5904\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199:Select Substring(@S1,PATINDEX(\u2019%\/\/%\u2019,@S1)+2,Len(@S1))
------------------------------------
\u663e\u793a\u7ed3\u679c: www.163.com
\u51fd\u6570PATINDEX\u4e0eCHARINDEX\u533a\u522b\u5728\u4e8e:\u524d\u8005\u53ef\u4ee5\u53c2\u6570\u4e00\u4e9b\u53c2\u6570,\u589e\u52a0\u67e5\u8be2\u7684\u529f\u80fd
\u65b9\u6cd5\u4e09:
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select REPLACE(@S1,\u2018http:\/\/\u2019,\u2019\u2019)
------------------------------------
\u663e\u793a\u7ed3\u679c: www.163.com
\u5229\u7528\u5b57\u7b26\u66ff\u6362\u51fd\u6570REPLACE,\u5c06\u9664\u9700\u8981\u663e\u793a\u5b57\u7b26\u4e32\u5916\u7684\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a
\u65b9\u6cd5\u56db:
Declare @S1 varchar(100)
Select @S1=\u2018http:\/\/www.163.com\u2019
Select STUFF(@S1,CHARINDEX(\u2018http:\/\/\u2019,@S1),Len(\u2018http:\/\/\u2019),\u2019\u2019)
------------------------------------
\u663e\u793a\u7ed3\u679c: www.163.com<\/p>\n

\u51fd\u6570STUFF\u4e0eREPLACE\u533a\u522b\u5728\u4e8e:\u524d\u8005\u53ef\u4ee5\u6307\u5b9a\u66ff\u6362\u8303\u56f4,\u800c\u540e\u8005\u5219\u662f\u5168\u90e8\u8303\u56f4\u5185\u66ff\u6362
B.\u622a\u53d6\u6307\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u622a\u53d6C:\\Windows\\test.txt\u4e2d\u6587\u4ef6\u540d)
\u4e0eA\u4e0d\u540c\u7684\u662f,\u5f53\u641c\u7d22\u5bf9\u8c61\u4e0d\u662f\u4e00\u4e2a\u65f6,\u5229\u7528\u4e0a\u9762\u7684\u65b9\u6cd5\u53ea\u80fd\u641c\u7d22\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e
\u65b9\u6cd5\u4e00:
Declare @S1 varchar(100)
Select @S1=\u2018C:\\Windows\\test.txt\u2019
select right(@S1,charindex(\u2019\u2019,REVERSE(@S1))-1)
-------------------------------------
\u663e\u793a\u7ed3\u679c: text.txt<\/p>\n

\u5229\u7528\u51fd\u6570REVERSE\u83b7\u53d6\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n

substr()
\u4f8b\u5b50:
private void DDL_AreaBind()
{
\n
conn = new SqlConnection(ConfigurationManager.ConnectionStrings[\u201cstrcon\u201d].ConnectionString);
string str = \u201c0000\u201d;
cmd = new SqlCommand(\u201cselect AreaID,Name=ltrim(Name) from Area where right(AreaID,4) =\u2019\u201d + str + \u201c\u2019\u201d, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds, \u201carea\u201d);
this.ddl_area.DataSource = ds.Tables[\u201carea\u201d].DefaultView;
this.ddl_area.DataTextField = \u201cName\u201d;
this.ddl_area.DataValueField = \u201cAreaID\u201d;
this.ddl_area.DataBind();<\/p>\n

          cmd = new SqlCommand(\"select * from Area    \", conn);\n          cmd.CommandType = CommandType.Text;\n          SqlDataAdapter adapter = new SqlDataAdapter(cmd);\n          adapter.Fill(ds, \"city\");\n          this.ddl_city.DataSource = ds.Tables[\"city\"].DefaultView;\n          this.ddl_city.DataTextField = \"Name\";\n          this.ddl_city.DataValueField = \"AreaID\";\n          this.ddl_city.DataBind();\n      }\n<\/code><\/pre>\n

protected void ddl_area_SelectedIndexChanged(object sender, EventArgs e)
{
\n
conn = new SqlConnection(ConfigurationManager.ConnectionStrings[\u201cstrcon\u201d].ConnectionString);
this.ddl_city.Enabled = true;
string str1=\u201c0000\u201d;
cmd = new SqlCommand(\u201cselect AreaID,Name from Area where substring(AreaID,1,2)=\u2019\u201d + this.ddl_area.SelectedValue.Substring(0,2) + \"\u2019 AND substring(AreaID,3,4) <> \u20180000\u2019 AND substring(AreaID,5,2)=\u201800\u2019 \", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, \u201ccity\u201d);
this.ddl_city.DataSource = ds.Tables[\u201ccity\u201d].DefaultView;
this.ddl_city.DataTextField = \u201cName\u201d;
this.ddl_city.DataValueField = \u201cAreaID\u201d;
this.ddl_city.DataBind();
}
PS\uff1a
\u6700\u8fd1\u9879\u76ee\u4e2d\u7528\u5230\u6bd4\u8f83\u5c11\u89c1\u7684SQL\u8bed\u53e5\uff0c\u5206\u4eab\u4e00\u4e0b\uff1a
\u67e5\u8be2\u7956\u5148\u8282\u70b9
select * from \u76ee\u5f55\u8868_\u6570\u636e\u5e93 where ID<>-1 and datatype<>1 and datatype<>2 connect by prior FATHERID=ID start with ID=28 order by \u76ee\u5f55\u7ea7\u522b,ID
\u67e5\u8be2\u5b50\u5b59\u8282\u70b9\uff1a
select * from \u76ee\u5f55\u8868_\u6570\u636e\u5e93 where ID<>-1 and datatype<>1 and datatype<>2 connect by prior ID=FATHERID start with ID=28 order by \u76ee\u5f55\u7ea7\u522b,ID<\/p>\n

SQL charindex\u7684\u7528\u6cd5
select * from table_a where charindex(\u2018a\u2019,id)>0 or charindex(\u2018b\u2019,id)>0
table_a \u8868\u4e2d id\u5b57\u6bb5\u4e2d\u542b\u6709\"a\"\u6216\u8005\"b\"\u7684\u8bb0\u5f55.<\/p>\n


\n

CHARINDEX
\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u8868\u8fbe\u5f0f\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002
\u8bed\u6cd5
CHARINDEX ( expression1 , expression2 [ , start_location ] )
\u53c2\u6570
expression1
\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5176\u4e2d\u5305\u542b\u8981\u5bfb\u627e\u7684\u5b57\u7b26\u7684\u6b21\u5e8f\u3002expression1 \u662f\u4e00\u4e2a\u77ed\u5b57\u7b26\u6570\u636e\u7c7b\u578b\u5206\u7c7b\u7684\u8868\u8fbe\u5f0f\u3002
expression2
\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u7528\u4e8e\u641c\u7d22\u6307\u5b9a\u5e8f\u5217\u7684\u5217\u3002expression2 \u5c5e\u4e8e\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u5206\u7c7b\u3002
start_location
\u5728 expression2 \u4e2d\u641c\u7d22 expression1 \u65f6\u7684\u8d77\u59cb\u5b57\u7b26\u4f4d\u7f6e\u3002\u5982\u679c\u6ca1\u6709\u7ed9\u5b9a start_location\uff0c\u800c\u662f\u4e00\u4e2a\u8d1f\u6570\u6216\u96f6\uff0c\u5219\u5c06\u4ece expression2 \u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u641c\u7d22\u3002
\u8fd4\u56de\u7c7b\u578b
int<\/p>\n

\u6ce8\u91ca
\u5982\u679c expression1 \u6216 expression2 \u4e4b\u4e00\u5c5e\u4e8e Unicode \u6570\u636e\u7c7b\u578b\uff08nvarchar \u6216 nchar\uff09\u800c\u53e6\u4e00\u4e2a\u4e0d\u5c5e\u4e8e\uff0c\u5219\u5c06\u53e6\u4e00\u4e2a\u8f6c\u6362\u4e3a Unicode \u6570\u636e\u7c7b\u578b\u3002
\u5982\u679c expression1 \u6216 expression2 \u4e4b\u4e00\u4e3a NULL \u503c\uff0c\u5219\u5f53\u6570\u636e\u5e93\u517c\u5bb9\u7ea7\u522b\u4e3a 70 \u6216\u66f4\u5927\u65f6\uff0cCHARINDEX \u8fd4\u56de NULL \u503c\u3002\u5f53\u6570\u636e\u5e93\u517c\u5bb9\u7ea7\u522b\u4e3a 65 \u6216\u66f4\u5c0f\u65f6\uff0cCHARINDEX \u4ec5\u5728 expression1 \u548c expression2 \u90fd\u4e3a NULL \u65f6\u8fd4\u56de NULL \u503c\u3002
\u5982\u679c\u5728 expression2 \u5185\u6ca1\u6709\u627e\u5230 expression1\uff0c\u5219 CHARINDEX \u8fd4\u56de 0\u3002<\/p>\n

\u793a\u4f8b
\u7b2c\u4e00\u4e2a\u4ee3\u7801\u793a\u4f8b\u8fd4\u56de\u5e8f\u5217\"wonderful\"\u5728 titles \u8868\u7684 notes \u5217\u4e2d\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4f7f\u7528\u53ef\u9009\u7684 start_location \u53c2\u6570\u4ece notes \u5217\u7684\u7b2c\u4e94\u4e2a\u5b57\u7b26\u5f00\u59cb\u5bfb\u627e\"wonderful\"\u3002\u7b2c\u4e09\u4e2a\u793a\u4f8b\u663e\u793a\u4e86\u5f53 expression2 \u5185\u627e\u4e0d\u5230 expression1 \u65f6\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n

USE pubs
GO
SELECT CHARINDEX(\u2019wonderful\u2019, notes)
FROM titles
WHERE title_id = \u2019TC3218\u2019
GO<\/p>\n

\u2013 Use the optional start_location parameter to start searching
\u2013 for wonderful starting with the fifth character in the notes
\u2013 column.
USE pubs
GO
SELECT CHARINDEX(\u2019wonderful\u2019, notes, 5)
FROM titles
WHERE title_id = \u2019TC3218\u2019
GO<\/p>\n

\u4e0b\u9762\u662f\u7b2c\u4e00\u4e2a\u67e5\u8be2\u548c\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff1a<\/h2>\n

46
(1 row(s) affected)
USE pubs
GO
SELECT CHARINDEX(\u2019wondrous\u2019, notes)
FROM titles
WHERE title_id=\u2019TC3218\u2019
GO<\/p>\n

\u4e0b\u9762\u662f\u7ed3\u679c\u96c6\u3002<\/p>\n


\n

0<\/p>\n

GPS\u5e73\u53f0\u3001\u7f51\u7ad9\u5efa\u8bbe\u3001\u8f6f\u4ef6\u5f00\u53d1\u3001\u7cfb\u7edf\u8fd0\u7ef4\uff0c\u627e\u68ee\u5927\u7f51\u7edc\u79d1\u6280\uff01
https:\/\/cnsendnet.taobao.com
\u6765\u81ea\u68ee\u5927\u79d1\u6280\u5b98\u65b9\u535a\u5ba2
http:\/\/www.cnsendblog.com\/index.php\/?p=2022<\/p>\n","protected":false},"excerpt":{"rendered":"GPS\u5e73\u53f0\u3001\u7f51\u7ad9\u5efa\u8bbe\u3001\u8f6f\u4ef6\u5f00\u53d1\u3001\u7cfb\u7edf\u8fd0\u7ef4\uff0c\u627e\u68ee\u5927\u7f51\u7edc\u79d1\u6280\uff01https:\/\/cnsendnet.taobao.com\u6765\u81ea\u68ee\u5927\u79d1\u6280\u5b98\u65b9\u535a\u5ba2http:\/\/www.cns...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/343"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=343"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/343\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}