sqlÖÐlpadº¯ÊýÓ÷¨
sql ÖÐ lpad º¯ÊýÓÃÓÚÔÚ×Ö·û´®×ó²àÌî³ä×Ö·ûÒÔµÖ´ïÖ¸¶¨³¤¶È¡£Óï·¨£ºlpad(source_string, target_length, pad_string)£¬ÆäÖÐ source_string ΪÔʼ×Ö·û´®£¬target_length ΪĿµÄ³¤¶È£¬pad_string ΪÌî³ä×Ö·û¡£ÀýÈ磬lpad(‘²âÊÔ’, 10, ‘0’) ½«ÔÚ ‘²âÊÔ’ ×ó²àÌî³ä 5 ¸ö ‘0’£¬ÒԵִﳤ¶ÈΪ 10 µÄ×Ö·û´® ‘00000²âÊÔ’¡£
SQL ÖÐ LPAD º¯ÊýÓ÷¨
LPAD º¯ÊýÓÃÓÚÔÚ×Ö·û´®µÄ×ó²àÌî³äÖ¸¶¨ÃüÄ¿µÄ×Ö·û¡£ËüÊÇÒ»ÖÖÃûÌû¯×Ö·û´®µÄÊÊÓú¯Êý£¬Í¨³£ÓÃÓÚ¶ÔÆëÎı¾»ò½¨ÉèÀο¿³¤¶ÈµÄ×Ö·û´®¡£
Óï·¨
LPAD(source_string, target_length, pad_string)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
source_string£ºÒªÌî³äµÄÔʼ×Ö·û´®¡£
target_length£ºÌî³äºó×Ö·û´®µÄÄ¿µÄ³¤¶È¡£
pad_string£ºÓÃÓÚÌî³äµÄ×Ö·û¡£
Ó÷¨
LPAD º¯Êýͨ¹ýÔÚ×Ö·û´®µÄ×ó²àÌí¼ÓÖظ´µÄ pad_string ×Ö·ûÀ´Ìî³ä×Ö·û´®¡£ÈôÊÇÔʼ×Ö·û´®µÄ³¤¶È´óÓÚÄ¿µÄ³¤¶È£¬Ôòº¯Êý»á½Ø¶ÏÔʼ×Ö·û´®ÒÔÆ¥ÅäÄ¿µÄ³¤¶È¡£
ÒÔÏÂʾÀýÑÝʾÁË LPAD º¯ÊýµÄÓ÷¨£º
SELECT LPAD('²âÊÔ', 10, '0') AS ÃûÌû¯×Ö·û´®;
µÇ¼ºó¸´ÖÆ
Êä³ö£º
ÃûÌû¯×Ö·û´® 00000²âÊÔ
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬Ôʼ×Ö·û´®Îª ‘²âÊÔ’£¬Ä¿µÄ³¤¶ÈΪ 10£¬Ìî³ä×Ö·ûΪ ‘0’¡£LPAD º¯ÊýÔÚ ‘²âÊÔ’ µÄ×ó²àÌî³äÁË 5 ¸ö ‘0’ ×Ö·û£¬Ê¹Æ䳤¶ÈµÖ´ï 10 ¸ö×Ö·û¡£
ʾÀý
ϱíÁгöÁ˸ü¶à LPAD º¯ÊýµÄʾÀý£º
Ô´×Ö·û´® | Ä¿µÄ³¤¶È | Ìî³ä×Ö·û | Êä³ö |
---|---|---|---|
‘123’ | 5 | ‘0’ | ‘00123’ |
‘ʾÀý’ | 15 | ‘ ‘ | ‘ ʾÀý’ |
‘ºÜ³¤ºÜ³¤µÄ×Ö·û´®’ | 20 | ‘_’ | ‘_ºÜ³¤ºÜ³¤µÄ×Ö·û´®’ |
½áÂÛ
LPAD º¯ÊýÊÇÒ»¸öÓÐÓõÄÃûÌû¯×Ö·û´®º¯Êý£¬¿ÉÓÃÓÚÔÚ×Ö·û´®µÄ×ó²àÌî³äÖ¸¶¨ÃüÄ¿µÄ×Ö·û¡£Í¨¹ýµ÷½â target_length ºÍ pad_string ¿ÉÒÔÎÞаµØ¿ØÖÆÌî³äµÄÐÐΪ¡£
ÒÔÉϾÍÊÇsqlÖÐlpadº¯ÊýÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡