Home Database Mysql Tutorial MySQL全角转换为半角_MySQL

MySQL全角转换为半角_MySQL

Jun 01, 2016 pm 01:04 PM
Half size


?
序言:
用户注册时候,录入了全角手机号码,所以导致短信系统根据手机字段发送短信失败,现在问题来了,如何把全角手机号码变成半角手机号码?



1,手机号码全角转换成半角
先查询出来全角半角都存在的手机号码的数据
SELECT a.username ,COUNT(1) AS num
FROM(
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(uu.user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9') AS username
FROM UC_USER uu WHERE uu.`USER_NAME` IS NOT NULL
)a GROUP BY a.username HAVING (COUNT(1)>1)
;
得到如下重复记录:
("MB.134xx76802x" ,
"MB.136xx88105x" ,
"MB.152xx80801x" ,
"MB.157xx49518x" ,
"MB.186xx88282x" ,
"MB.189xx94855x" ); )


然后删除掉已经存在半角的全角手机号码记录,不然转换后会有重复的手机号码。
DELETE FROM `UC_USER`
WHERE MOBILE LIKE '%1%'
AND REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9')
IN("MB.134xx76802x" ,
"MB.136xx88105x" ,
"MB.152xx80801x" ,
"MB.157xx49518x" ,
"MB.186xx88282x" ,
"MB.189xx94855x" );


之后再修改全角手机号码为半角手机号码
UPDATE UC_USER uu
SET uu.`MOBILE`=REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(uu.`MOBILE`,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9'),
uu.`USER_NAME`=REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(uu.user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9')
WHERE uu.`MOBILE` IS NOT NULL;


2,如何把所以的全角转换成半角
上面只是人为用比较笨拙的10个replace将全角转换成了半角,有没有一种通用的思路或者方法来实现呢?于是google了很多资料,写下如下的存储函数。
DELIMITER $$
USE csdn $$
CREATE FUNCTION `csdn`.`func_convert`(p_str VARCHAR(200),flag INT)
RETURNS VARCHAR(200)
BEGIN
DECLARE pat VARCHAR(8);
DECLARE step INT ;
DECLARE i INT ;
DECLARE spc INT;
DECLARE str VARCHAR(200);

SET str=p_str;
IF flag=0 THEN /**全角换算半角*/
SET pat= N'%[!-~]%' ;
SET step= -65248 ;
SET str = REPLACE(str,N' ',N' ');
ELSE /**半角换算全角*/
SET pat= N'%[!-~]%' ;
SET step= 65248 ;
SET str= REPLACE(str,N' ',N' ') ;
END IF;

SET i=LOCATE(pat,str) ;
loop1:WHILE i>0 DO
/**开始将全角转换成半角*/
SET str= REPLACE(str, SUBSTRING(str,i,1), CHAR(UNICODE(SUBSTRING(str,i,1))+step));
SET i=INSTR(str,pat) ;
END WHILE loop1;
RETURN(str)
END $$
DELIMITER ;



3,google出来的sqlserver中的全角半角转换函数。
DELIMITER $$


CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
FUNCTION `test`.`u_convert`(@str NVARCHAR(4000),@flag BIT )
RETURNS NVARCHAR
BEGIN
DECLARE @pat NVARCHAR(8);
DECLARE @step INTEGER;
DECLARE @i INTEGER;
DECLARE @spc INTEGER;
IF @flag=0
BEGIN
SELECT N'%[!-~]%' INTO @pat;
SELECT -65248 INTO @step;
SELECT REPLACE(@str,N' ',N' ') INTO @str;
END
ELSE
BEGIN
SELECT N'%[!-~]%' INTO @pat;
SELECT 65248 INTO @step;
SELECT REPLACE(@str,N' ',N' ') INTO @str;
END
SELECT patindex(@pat COLLATE LATIN1_GENERAL_BIN,@str) INTO @i;
WHILE @i>0 DO
SELECT REPLACE(@str, SUBSTRING(@str,@i,1), NCHAR(UNICODE(SUBSTRING(@str,@i,1))+@step)) INTO @str;
SELECT patindex(@pat COLLATE LATIN1_GENERAL_BIN,@str) INTO @i;
END WHILE
RETURN(@str)
END $$
DELIMITER ;


----------------------------------------------------------------------------------------------------------------

原博客地址: http://blog.itpub.net/26230597/viewspace-1316445/
原作者:黄杉 (mchdba)
----------------------------------------------------------------------------------------------------------------

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between full-width and half-width The difference between full-width and half-width Jan 05, 2024 pm 04:27 PM

The difference between full-width and half-width: 1. The space occupied by characters; 2. The representation of characters; 3. The status of the input method; 4. Essential differences; 5. The use of system commands. Detailed introduction: 1. Character occupation space, full-width characters occupy two standard character positions, while half-width characters occupy one standard character position; 2. Character representation, full-width refers to one character occupying two standard character positions, whether it is a Chinese character Or other types of characters, and half-width means that a character occupies a standard character position, usually used for English letters, numbers, symbols, etc.

Understanding full-width and half-width: a look at switching techniques Understanding full-width and half-width: a look at switching techniques Mar 25, 2024 pm 01:36 PM

In daily life, we often encounter the problem of full-width and half-width, but few people may have a deep understanding of their meaning and difference. Full-width and half-width are actually concepts of character encoding methods, and they have special applications in computer input, editing, typesetting, etc. This article will delve into the differences between full-width and half-width, switching techniques, and real-life applications. First of all, the definitions of full-width and half-width in the field of Chinese characters are: a full-width character occupies one character position, and a half-width character occupies half a character position. In a computer, pass

Easily learn how to switch between full-width and half-width Easily learn how to switch between full-width and half-width Mar 25, 2024 pm 03:42 PM

Full-width and half-width refer to two different input states in the Chinese input method. Full-width means that each character occupies a full-width character position, while half-width means that each character occupies a half-width character position. When using a computer to input Chinese, sometimes you need to switch between full-width and half-width to adapt to different input scenarios. Next, we will introduce several commonly used full-width and half-width switching methods, so that everyone can easily learn how to switch between full-width and half-width in Chinese input. Method 1: Use shortcut keys to switch between full-width and half-width. In most Chinese input method software, you can use

What is the difference between full-width and half-width input methods? What is the difference between full-width and half-width input methods? Feb 14, 2024 pm 02:00 PM

When we use the Chinese input method to type, there are two ways: full-width and half-width. Among them, there are many friends who don’t know the difference between full-width and half-width. Let’s take a look at the full-width and half-width input methods. Make the difference. Any punctuation mark can be used in the half-width state, and there are no special restrictions on spaces. In the full-width state, the function of the space bar changes. After switching to the full-width state and pressing the space bar, we will find that the distance between characters becomes large, and this gap is more obvious than in the half-width state. In addition, the display of English letters is also different in the full-width state. In the half-width state, the typed English letters are of normal size. However, once we switch to full-width mode, we will find that the typed English letters become bold and enlarged. in the whole

Shortcut keys for switching between full-width and half-width in Windows Shortcut keys for switching between full-width and half-width in Windows Mar 04, 2024 pm 06:52 PM

When users usually use input methods to insert text, they may notice differences between letters or symbols. This is mainly due to the different switching between half-width and full-width characters. Here, we will introduce you in detail how to quickly realize the conversion between full-width and half-width. Shortcut key solution for switching between full-width and half-width in windows: Alt+Shift This is the most commonly used key to switch between full-width and half-width input modes. In the Chinese input environment, pressing this key will switch to English mode, otherwise it will switch to Chinese mode. If there are multiple input methods installed on your computer, you can also use this key to easily switch between input methods. Solution 2: Ctrl+Shift is also among the shortcut keys for switching between full-width and half-width. This key combination is pressed in English input mode

Quickly master the skills of switching between full-width and half-width Quickly master the skills of switching between full-width and half-width Mar 25, 2024 am 10:57 AM

In the modern Internet era, we often enter and edit text on computers. In this process, we sometimes encounter the problem of switching between full-width and half-width. Full-width and half-width refer to the size of the position occupied by characters. Full-width characters occupy one character width, while half-width characters occupy half a character width. The correct use of full-width and half-width is very important to ensure the formatting and uniformity of text. Therefore, it is necessary to master the switching skills of full-width and half-width. First, let us understand the concepts of full-width and half-width. Full-width characters generally refer to

Detailed explanation of the functional features of full-width spaces and half-width spaces Detailed explanation of the functional features of full-width spaces and half-width spaces Mar 25, 2024 pm 07:21 PM

Full-width spaces and half-width spaces are two common space characters in Chinese and Japanese. They have different functional characteristics in typesetting, text editing, and input methods. This article will explain in detail the characteristics and application scenarios of full-width spaces and half-width spaces, helping readers better understand and use these two space characters. First, let’s understand the characteristics of full-width spaces. The encoding of full-width spaces in Unicode is U+3000. The width is the same as that of ordinary Chinese characters. It is usually used for blank spaces in Chinese typesetting. Usage scenarios for full-width spaces include but

How to easily switch between full-width and half-width modes in the input method How to easily switch between full-width and half-width modes in the input method Mar 25, 2024 pm 10:00 PM

Title: How to easily switch between full-width and half-width modes in input methods When using computers daily, we often need to switch between full-width and half-width to adapt to different input scenarios and needs. Full-width mode is generally used to input Chinese characters, English characters, punctuation marks, numbers, etc., while half-width mode is more suitable for inputting English and numbers. However, many people may be confused when using input methods and don't know how to easily switch between full-width and half-width. This article will introduce some simple methods to help you easily switch between full-width and half-width input methods.

See all articles