简体   繁体   English

如何使用 SQL 查询在列的每一行中添加加号(+)?

[英]How to add plus(+) sign in every row of a column using SQL query?

I have a database with a column containing phone numbers.我有一个数据库,其中有一列包含电话号码。 I want to add plus(+) sign in the beginning of every phone number.我想在每个电话号码的开头添加加号(+)。

UPDATE Table 
SET Phone = CONCAT('+', Phone)

This is the query I'm using to insert any other character, but it doesn't seem to work with + sign.这是我用来插入任何其他字符的查询,但它似乎不适用于 + 号。 It says 0 row(s) affected Rows matched: 4023 Changed: 0 Warnings: 0 , which means nothing has changed.它说0 行受影响 匹配的行:4023 已更改:0 警告:0 ,这意味着没有任何改变。

Even if I do SET Phone = CONCAT('+91', Phone) , only 91 is being inserted and not the plus sign.即使我这样做SET Phone = CONCAT('+91', Phone) ,也只插入91而不是加号。

I think I understood what's going on here.我想我明白这里发生了什么。 Your Table is probably using integer data type for Phone column.您的Table可能对Phone列使用整数数据类型。 In this case Phone = CONCAT('+91', Phone) implicitly converts value to character type, adds leading "+" but then converts it back to integer type to store value.在这种情况下Phone = CONCAT('+91', Phone)将值隐式转换为字符类型,添加前导“+”,然后将其转换回整数类型以存储值。

You might want to do something like你可能想做类似的事情

ALTER TABLE `Table` MODIFY Phone Varchar(100);

(please choose column type accordingly) (请相应选择列类型)

I reproduced it here http://sqlfiddle.com/#!9/5b3a651/1我在这里转载了它http://sqlfiddle.com/#!9/5b3a651/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM