{"id":364,"date":"2023-09-16T10:44:50","date_gmt":"2023-09-16T02:44:50","guid":{"rendered":""},"modified":"2023-09-18T11:50:02","modified_gmt":"2023-09-18T03:50:02","slug":"sql%e5%b8%b8%e7%94%a8%e5%87%bd%e6%95%b0%e8%af%a6%e8%a7%a3%ef%bc%88%e4%b8%80%ef%bc%89-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96","status":"publish","type":"post","link":"https:\/\/mushiming.com\/364.html","title":{"rendered":"sql\u5e38\u7528\u51fd\u6570\u8be6\u89e3\uff08\u4e00\uff09\u2014\u2014\u5b57\u7b26\u4e32\u622a\u53d6"},"content":{"rendered":"
SUBSTRING ( expression, start, length )<\/p>\n
expression\u2014\u2014\u5b57\u7b26\u4e32\u3001\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3001\u6587\u672c\u3001\u56fe\u50cf\u3001\u5217\u6216\u5305\u542b\u5217\u7684\u8868\u8fbe\u5f0f\u3002\u8bf7\u52ff\u4f7f\u7528\u5305\u542b\u805a\u5408\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n
start\u2014\u2014\u6574\u6570\u6216\u53ef\u4ee5\u9690\u5f0f\u8f6c\u6362\u4e3aint \u7684\u8868\u8fbe\u5f0f\uff0c\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002<\/p>\n
length\u2014\u2014\u6574\u6570\u6216\u53ef\u4ee5\u9690\u5f0f\u8f6c\u6362\u4e3a int \u7684\u8868\u8fbe\u5f0f\uff0c\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n
left(field,length) \u2014\u2014\u51fd\u6570\u8fd4\u56defield\u6700\u5de6\u8fb9\u7684length\u4e2a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32<\/p>\n
right(field,length)\u2014\u2014\u51fd\u6570\u8fd4\u56defield\u6700\u53f3\u8fb9\u7684length\u4e2a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32<\/p>\n
eg1:\u53d6\u5b9a\u957f\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32<\/p>\n
select substring('abdcsef',1,3)<\/code><\/pre>\nresult: abd<\/p>\n
<\/p>\n
2.CHARINDEX\u51fd\u6570\u2014\u2014\u8fd4\u56de\u5b57\u7b26\u6216\u8005\u5b57\u7b26\u4e32\u5728\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e<\/h3>\n <\/code><\/pre>\nCHARINDEX ( expression1 , expression2 [ , start_location ] )<\/strong> \u2014\u2014 CHARINDEX\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u8fd4\u56de\u7684\u6574\u6570\u662f\u8981\u627e\u7684\u5b57\u7b26\u4e32\u5728\u88ab\u627e\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002Expression1\u662f\u8981\u5230expression2\u4e2d\u5bfb\u627e\u7684\u5b57\u7b26\u4e2d\uff0cstart_location\u662fCHARINDEX\u51fd\u6570\u5f00\u59cb\u5728expression2\u4e2d\u627eexpression1\u7684\u4f4d\u7f6e\u3002 <\/strong><\/p>\nCHARINDEX('SQL', 'Microsoft SQL Server')<\/code><\/pre>\n\u8fd9\u4e2a\u51fd\u6570\u547d\u4ee4\u5c06\u8fd4\u56de\u5728\u201cMicrosoft SQL Server\u201d\u4e2d\u201cSQL\u201d\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cCHARINDEX\u51fd\u6570\u5c06\u8fd4\u56de\u201cS\u201d\u5728\u201cMicrosoft SQL Server\u201d\u4e2d\u7684\u4f4d\u7f6e11\u3002<\/p>\n
eg1:\u53d6\u4e0d\u5b9a\u957f\u4e0d\u540c\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32<\/p>\n
<\/p>\n
\u6211\u4eec\u53ea\u60f3\u8981\u5f97\u5230'roomno'\u4e2d\u7684\u623f\u95f4\u53f7\uff0c\u53d1\u73b0\u8d77\u59cb\u5b57\u7b26\u4f4d\u7f6e\u5e76\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\uff0c\u6211\u4eec\u9700\u8981\u7684\u623f\u95f4\u53f7\u957f\u5ea6\u4e5f\u4e0d\u56fa\u5b9a\u3002<\/p>\n
\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u7528\u2018charindex\u2019\u8fd9\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u8f7b\u677e\u641e\u5b9a\uff0c\u5b83\u662f\u7528\u6765\u5b9a\u4f4d\u67d0\u4e2a\u7279\u5b9a\u5b57\u7b26\u5728\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5373\u8be5\u51fd\u6570<\/p>\n
\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u7528\u6765\u8868\u793a\u67d0\u4e2a\u7279\u5b9a\u5b57\u7b26\u4f4d\u7f6e\u7684\u6570\u5b57\u3002\u6267\u884c\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n
select \nroom_stand=substring(roomno,charindex('\u5143',roomno)+1,charindex('\u5ba4',roomno)-charindex('\u5143',roomno)-1) \nfrom PROPERTY_room\nwhere roomno like '%\u5355\u5143%\u5ba4%' <\/code><\/pre>\n result:<\/p>\n
<\/p>\n
3.STUFF\u51fd\u6570\u2014\u2014\u5220\u9664\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\uff0c\u5e76\u5728\u6307\u5b9a\u7684\u8d77\u70b9\u5904\u63d2\u5165\u53e6\u4e00\u7ec4\u5b57\u7b26\u3002<\/h3>\n
STUFF ( character_expression , start , length ,character_expression )<\/p>\n
SELECT STUFF('abcde', 2, 3, '123')<\/code><\/pre>\n result\uff1a a123e<\/p>\n
4.patindex\u51fd\u6570\u2014\u2014<\/span>\u8fd4\u56depattern\u5b57\u7b26\u4e32\u5728\u8868\u8fbe\u5f0fexpression\u91cc\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u8d77\u59cb\u503c\u4ece1\u5f00\u59cb\u7b97\u3002<\/h3>\n1. PATINDEX ( '%pattern%' , expression )\u2014\u2014'%pattern%'\u7684\u7528\u6cd5\u7c7b\u4f3c\u4e8e like '%pattern%'\u7684\u7528\u6cd5\uff0c\u4e5f\u5c31\u662f\u6a21\u7cca\u67e5\u627e\u5176pattern\u5b57\u7b26\u4e32\u662f\u5426\u662fexpression\u627e\u5230\uff0c\u627e\u5230\u5e76\u8fd4\u56de\u5176\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/p>\n
eg1\uff1a<\/p>\n
select patindex('%abb%','abcaabbeeabb')<\/code><\/pre>\n\u7ed3\u679c5\uff0c\u4e5f\u5c31\u662fabb\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/p>\n
2.PATINDEX ( '%pattern' , expression )\u2014\u2014'%pattern' \u7c7b\u4f3c\u4e8e like '%pattern'\u7684\u7528\u6cd5\uff0c\u524d\u9762\u7528\u6a21\u7cca\u67e5\u627e\uff0c\u4e5f\u5c31\u662f\u67e5\u627epattern\u7684\u7ed3\u675f\u6240\u5728expression\u7684\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u4ece\u540e\u9762\u5339\u914d\u8d77\u67e5\u627e\u3002<\/p>\n
eg2\uff1a<\/p>\n
select patindex('%abb','abcaabbeeabb')<\/code><\/pre>\n\u8fd4\u56de10\uff0c\u4e5f\u5c31\u662fabb\u5728\u540e\u9762\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/p>\n
select patindex('%abb','abcaabbeeabbr')<\/code><\/pre>\n\u8fd4\u56de0\uff0c\u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcdr\u548cabb\u4e0d\u5339\u914d\uff0c\u6240\u4ee5\u8fd4\u56de0<\/p>\n
3.PATINDEX ( 'pattern%' , expression )\u2014\u2014\u8fd9\u5c31\u76f8\u5f53\u4e8e\u7cbe\u786e\u5339\u914d\u67e5\u627e\uff0c\u4e5f\u5c31\u662fpattern,expression\u5b8c\u5168\u76f8\u7b49\u3002<\/p>\n
eg3\uff1a<\/p>\n
select patindex('abb%','abbcaabbeeabb')<\/code><\/pre>\n\u8fd4\u56de1\uff0c\u4e5f\u5c31\u76f8\u5f53\u4e8e\u8d77\u59cb\u503c<\/p>\n
select patindex('abb%','arbbcaabbeeabb')<\/code><\/pre>\n\u8fd4\u56de0\uff0c\u5f00\u5934\u627e\u4e0d\u5230\u5c31\u8fd4\u56de0,\u540e\u9762\u65e0\u8bba\u6709\u591a\u5c11\u90fd\u4e0d\u7ba1\u3002<\/p>\n
4.PATINDEX ( 'pattern' , expression )\u2014\u2014\u8fd9\u5c31\u76f8\u5f53\u4e8e\u7cbe\u786e\u5339\u914d\u67e5\u627e\uff0c\u4e5f\u5c31\u662fpattern,expression\u5b8c\u5168\u76f8\u7b49\u3002<\/p>\n
select patindex('abb','abb')<\/code><\/pre>\n\u8fd4\u56de1,\u5b8c\u5168\u76f8\u7b49<\/p>\n
select patindex('abb','abbr')<\/code><\/pre>\n\u8fd4\u56de0,\u4e0d\u5b8c\u5168\u76f8\u7b49<\/p>\n
5. patindex('%[pattern]%','ddabcaabbeeabbr')\uff0c<\/h2>\n patindex('%[^pattern]%','ddabcaabbeeabbr')<\/h2>\n
\u5728\u6b64\u5148\u8bf4[]\u7684\u7528\u6cd5\uff0c[]\u662f\u6307\u5b9a\u67d0\u4e9b\u7279\u6b8a\u7684\u5b57\u7b26\u3002[^]\u9664[]\u4e4b\u5916\u7684\u5b57\u7b26\u4e32,[]\u8fd9\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a<\/p>\n
\u5982\uff1a<\/p>\n
Symbol Meaning<\/p>\n
LIKE '5[%]' 5%<\/p>\n
LIKE '[_]n' _n<\/p>\n
LIKE '[a-cdf]' a, b, c, d, or f<\/p>\n
LIKE '[-acdf]' -, a, c, d, or f<\/p>\n
LIKE '[ [ ]' [<\/p>\n
LIKE ']' ]<\/p>\n
LIKE 'abc[_]d%' abc_d and abc_de<\/p>\n
LIKE 'abc[def]' abcd, abce, and abcf<\/p>\n
\u5982\uff1a<\/p>\n
select patindex('%[d]%','rcrdsddddaadadffdr')<\/p>\n
\u8fd4\u56de4\uff0c[]\u4e2dd\u5728\u5b57\u7b26\u4e32rcrdsddddaadadffdr\u7684\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/p>\n
select patindex('%[cd]%','rcrdsdddrdaadadffdr')<\/p>\n
\u8fd4\u56de2\uff0c[]\u4e2dc,d\u5728\u5176\u4e2d\u4e00\u4e2a\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u6700\u5148\u51fa\u73b0\u7684\u8fd9\u4e2a\u4f4d\u7f6e\uff0cc\u5728\u6b64\u5b57\u7b26\u4e32\u91cc\u7b2c\u4e00\u6b21\u51fa\u73b0\u4f4d\u7f6e\u662f2\uff0c\u800cd\u662f4\uff0c\u7ed3\u679c\u53d6\u6700\u5148\u7684\u90a3\u4e2a\u3002<\/p>\n
select patindex('%[sd]%','rcrdsdddrdaadadffdr')<\/p>\n
\u8fd4\u56de4\uff0c[]\u4e2dc,d\u5728\u5176\u4e2d\u4e00\u4e2a\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u6700\u5148\u51fa\u73b0\u7684\u8fd9\u4e2a\u4f4d\u7f6e\uff0cs\u5728\u6b64\u5b57\u7b26\u4e32\u91cc\u7b2c\u4e00\u6b21\u51fa\u73b0\u4f4d\u7f6e\u662f5\uff0c\u800cd\u662f4\uff0c\u7ed3\u679c\u53d6\u6700\u5148\u7684\u90a3\u4e2a\u3002<\/p>\n
select patindex('%[^r]%','rrrdsdddrdaadadffdr')<\/p>\n
\u8fd4\u56de4\uff0c\u9664[]\u4e2d\u7684\u5b57\u7b26\u4e32\u7684\u5339\u914d\u5b57\u7b26\uff0c\u7b2c\u4e00\u6b21\u51fa\u73b0d\u4e0d\u5728[^r]\u91cc\uff0c\u6240\u4ee5\u5c31\u627e\u5230\u7b2c\u4e00\u6b21\u8fd9\u4f4d\u3002<\/p>\n
select patindex('%[^rd]%','rrrdsdddrdaadadffdr')<\/p>\n
\u8fd4\u56de5\uff0c\u9664[]\u4e2d\u7684\u5b57\u7b26\u4e32\u7684\u5339\u914d\u5b57\u7b26\uff0c\u7b2c\u4e00\u6b21\u51fa\u73b0s\u4e0d\u5728[^rd]\u91cc\uff0c\u6240\u4ee5\u5c31\u627e\u5230\u7b2c\u4e00\u6b21\u8fd9\u4f4d\u3002<\/p>\n
select patindex('%[^rsd]%','rrrdsdddrdaadadffdr')<\/p>\n
\u8fd4\u56de11\uff0c\u9664[]\u4e2d\u7684\u5b57\u7b26\u4e32\u7684\u5339\u914d\u5b57\u7b26\uff0c\u7b2c\u4e00\u6b21\u51fa\u73b0a\u4e0d\u5728[^rsd]\u91cc\uff0c\u6240\u4ee5\u5c31\u627e\u5230\u7b2c\u4e00\u6b21\u8fd9\u4f4d\u3002<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
eg\uff1a\u53bb\u9664\u62ec\u53f7\u53ca\u62ec\u53f7\u5185\u5185\u5bb9\uff08\u6279\u91cf\u5316\u5904\u7406\uff09<\/h3>\n--> \u6d4b\u8bd5\u6570\u636e\uff1a#\nif object_id('tempdb.dbo.#') is not null drop table #\u65b0\u5efa\u4e34\u65f6\u8868\ncreate table #(col varchar(46))\ninsert into #\nselect '\u91cd\u5e86\u552f\u8fdc\u5b9e\u4e1a\u6709\u9650\u516c\u53f8(2009\u5e7401\u670805\u65e5)' union all\nselect '\u6c5f\u82cf\u82cf\u7f8e\u8fbe\u8239\u8236\u5de5\u7a0b\u6709\u9650\u516c\u53f8(2009\u5e7401\u670805\u65e5)' union all\nselect '\u4e0a\u6d77\u542f\u95e8\u673a\u7535\u6709\u9650\u516c\u53f8(2009\u5e7401\u670804\u65e5)' union all\nselect '\u5c0f\u6d0b\u4eba\u751f\u7269\u4e73\u4e1a\u96c6\u56e2\u6709\u9650\u516c\u53f8(2009\u5e7401\u670804\u65e5)' union all\nselect '\u6c88\u9633\u9f0e\u51b7\u673a\u7535\u8bbe\u5907\u6709\u9650\u516c\u53f8(2009\u5e7401\u670804\u65e5)' union all\nselect '\u5609\u67cf(\u4e2d\u56fd)\u56fd\u9645\u8d27\u8fd0\u4ee3\u7406\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)' union all\nselect '\u5e7f\u5dde\u5b9d\u6d01\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)' union all\nselect '\u70df\u53f0\u534e\u79d1\u98df\u54c1\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)' union all\nselect '\u827e\u6765\u5f97\u673a\u68b0(\u4e0a\u6d77)\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)' union all\nselect '\u4e0a\u6d77\u6653\u821f\u8239\u8236\u914d\u4ef6\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)' union all\nselect '\u4e0a\u6d77\u529b\u5f18\u5305\u88c5\u5668\u6750\u6709\u9650\u516c\u53f8(2008\u5e7412\u670831\u65e5)'\n\n\n\/*\n------------------------------\n\u91cd\u5e86\u552f\u8fdc\u5b9e\u4e1a\u6709\u9650\u516c\u53f8\n\u6c5f\u82cf\u82cf\u7f8e\u8fbe\u8239\u8236\u5de5\u7a0b\u6709\u9650\u516c\u53f8\n\u4e0a\u6d77\u542f\u95e8\u673a\u7535\u6709\u9650\u516c\u53f8\n\u5c0f\u6d0b\u4eba\u751f\u7269\u4e73\u4e1a\u96c6\u56e2\u6709\u9650\u516c\u53f8\n\u6c88\u9633\u9f0e\u51b7\u673a\u7535\u8bbe\u5907\u6709\u9650\u516c\u53f8\n\u5609\u67cf(\u4e2d\u56fd)\u56fd\u9645\u8d27\u8fd0\u4ee3\u7406\u6709\u9650\u516c\u53f8\n\u5e7f\u5dde\u5b9d\u6d01\u6709\u9650\u516c\u53f8\n\u70df\u53f0\u534e\u79d1\u98df\u54c1\u6709\u9650\u516c\u53f8\n\u827e\u6765\u5f97\u673a\u68b0(\u4e0a\u6d77)\u6709\u9650\u516c\u53f8\n\u4e0a\u6d77\u6653\u821f\u8239\u8236\u914d\u4ef6\u6709\u9650\u516c\u53f8\n\u4e0a\u6d77\u529b\u5f18\u5305\u88c5\u5668\u6750\u6709\u9650\u516c\u53f8\n*\/\n<\/code><\/pre>\n\u89e3\u51b3\u65b9\u6848\u4e00\uff1a<\/p>\n
select reverse(stuff(reverse(col), 1, charindex('(', reverse(col)), '')) from #\n<\/code><\/pre>\n\u89e3\u51b3\u65b9\u6848\u4e8c\uff1a<\/p>\n
update tb\nset col = reverse(substring(reverse(col) ,charindex('(',reverse(col)) + 1 , len(col)))\nselect * from tb<\/code><\/pre>\n\u89e3\u51b3\u65b9\u6848\u4e09\uff1a<\/p>\n
select \nleft(name,patindex('%([0-9][0-9][0-9][0-9]\u5e74%',name)-1) as name from [tb]\n<\/code><\/pre>\n\u89e3\u51b3\u65b9\u6848\u56db\uff1a<\/p>\n
select left(col,len(col) - 13) from tb<\/code><\/pre>\n <\/p>\n","protected":false},"excerpt":{"rendered":"1.substring\u51fd\u6570\u2014\u2014\u622a\u53d6\u5b57\u7b26\u4e32SUBSTRING(expression,start,length)expression\u2014\u2014\u5b57\u7b26\u4e32\u3001\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3001\u6587\u672c\u3001\u56fe\u50cf\u3001\u5217\u6216\u5305\u542b\u5217...","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\/364"}],"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=364"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/364\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}