sqlÖÐÌæ»»Ö¸¶¨Î»ÖõÄ×Ö·ûÔõôŪ
ÔÚ sql ÖУ¬Ê¹Óà substr() º¯ÊýÖ¸¶¨ÒªÌæ»»×Ö·ûµÄÆðʼλÖúͳ¤¶È£¬È»ºóʹÓà replace() º¯ÊýÌæ»»Ö¸¶¨Î»ÖõÄ×Ö·û£¬Ó﷨Ϊ replace(string, start, length, new_string)¡£
SQL ÖÐÌæ»»Ö¸¶¨Î»ÖõÄ×Ö·û
ÔÚ SQL ÖУ¬¿ÉÒÔʹÓà SUBSTR() º¯ÊýºÍ REPLACE() º¯ÊýÀ´Ìæ»»Ö¸¶¨Î»ÖõÄ×Ö·û¡£
Óï·¨
REPLACE(string, start, length, new_string)
µÇ¼ºó¸´ÖÆ
²ÎÊý
string£ºÒªÌæ»»×Ö·ûµÄÔʼ×Ö·û´®¡£
start£ºÒªÌæ»»×Ö·ûµÄÆðʼλÖã¨´Ó 1 ×îÏȼÆÊý£©¡£
length£ºÒªÌæ»»×Ö·ûµÄ³¤¶È¡£
new_string£ºÓÃÓÚÌæ»»µÄ×Ö·û´®¡£
ʾÀý
Ìæ»»×Ö·û´®ÖÐ´ÓµÚ 5 λ×îÏ鵀 3 ¸ö×Ö·ûΪ “ABC”:
SELECT REPLACE('Hello World', 5, 3, 'ABC');
µÇ¼ºó¸´ÖÆ
Êä³ö£º
Hello ABCld
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
SUBSTR() º¯Êý
SUBSTR() º¯Êý¿ÉÓÃÓÚÖ¸¶¨ÒªÌæ»»µÄ×Ö·ûµÄÆðʼλÖúͳ¤¶È£º
Óï·¨
SUBSTR(string, start, length)
µÇ¼ºó¸´ÖÆ
²ÎÊý
string£ºÒªÌáÈ¡×Ó×Ö·û´®µÄÔʼ×Ö·û´®¡£
start£ºÒªÌáÈ¡×Ó×Ö·û´®µÄÆðʼλÖã¨´Ó 1 ×îÏȼÆÊý£©¡£
length£ºÒªÌáÈ¡×Ó×Ö·û´®µÄ³¤¶È¡£
ʾÀý
ʹÓà SUBSTR() º¯ÊýÖ¸¶¨ÒªÌæ»»µÄ×Ó×Ö·û´®£º
SELECT REPLACE('Hello World', SUBSTR('Hello World', 5, 3), 'ABC');
µÇ¼ºó¸´ÖÆ
Êä³ö£º
Hello ABCld
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖÐÌæ»»Ö¸¶¨Î»ÖõÄ×Ö·ûÔõôŪµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡