简体   繁体   English

在SQL数据库上整理手机号码

[英]Sorting out mobile numbers on SQL database

All the mobile numbers have gone into the format 447XXXXXXXX, I would like the numbers to be in the format of 07XXXXXXXXXX. 所有手机号码都采用447XXXXXXXX的格式,我希望号码采用07XXXXXXXXXX的格式。

I was going to try the following query: 我打算尝试以下查询:

UPDATE stack
SET Telephone= REPLACE(Telephone,'447','07')

However, if a number contains 447 this will also be replaced, I was thinking to use wildcards to replace just the first two 4's of every column. 但是,如果一个包含447的数字也将被替换,我在考虑使用通配符替换每列的前两个4。 How would be the best way to update all 49,000 entries, I have created a backup just in case. 为了最好地更新所有49,000个条目,我已经创建了一个备份,以防万一。

UPDATE UPDATE

It seems the numbers were set in the wrong format so the following query was used to put them in the correct format: 似乎数字设置的格式错误,因此使用以下查询将其设置为正确的格式:

ALTER TABLE stack ALTER COLUMN Telephone VARCHAR(15)

Now ALL the numbers are showing up in a weird format: 4.47944e+011 现在所有数字都以奇怪的格式显示:4.47944e + 011

Change your data type to varchar (number data types don't store leading zeros). 将数据类型更改为varchar (数字数据类型不存储前导零)。 To do that you need to change it to bigint first, to make the conversion work: 为此,您需要先将其更改为bigint ,以使转换生效:

ALTER TABLE your_table ALTER COLUMN Telephone BIGINT;
ALTER TABLE your_table ALTER COLUMN Telephone VARCHAR(15);

After that you can change your data like this 之后,您可以像这样更改数据

UPDATE your_table
SET Telephone = '07' + substring(Telephone, 4,99)
WHERE charindex('447', Telephone) = 1

SQLFiddle demo SQLFiddle演示

Maybe change the source (and avoid conversion requirements in future?): 也许更改源(并避免将来转换要求?):

=IF(LEFT(A1,2)="44",0&MID(A1,3,15),LEFT(A1,15))  

This results in a string. 这将导致一个字符串。

Following should work. 以下应该工作。 I gave an example as well... You need to only use the UPDATE statement... 我也举了一个例子...您只需要使用UPDATE语句...

DECLARE @Telephone TABLE(Telephone VARCHAR(100))

INSERT INTO @Telephone 
SELECT '44712345' UNION ALL
SELECT '123447' UNION ALL
SELECT '447123447' 

SELECT *
FROM @Telephone

UPDATE @Telephone
SET Telephone = STUFF(Telephone, 1, 3, '07')
WHERE (charindex('447', Telephone) = 1)

SELECT *
FROM @Telephone

1) Phone number are stored (usually) using a VARCHAR column. 1)通常使用VARCHAR列存储电话号码。

2) Assuming that the old Telephone column was a FLOAT column, if you Telehone column (now a VARCHAR(15) column) has values like '4.47944e+011' then you need to convert these values back to FLOAT and then you have to use STR to convert FLOAT values to VARCHAR. 2)假设旧的 Telephone列是FLOAT列,如果Telehone列(现在是VARCHAR(15)列)具有类似于'4.47944e+011'则需要将这些值转换回FLOAT,然后必须使用STR将FLOAT值转换为VARCHAR。 For example, following script 例如,以下脚本

DECLARE @PhoneFromFloat VARCHAR(15) = '4.47944e+011'
SELECT STR(CONVERT(FLOAT, @PhoneFromFloat), 15, 0)

outputs: 447944000000 . 输出: 447944000000

To execute this update you could use this script: 要执行此更新,您可以使用以下脚本:

BEGIN TRANSACTION;

UPDATE  dbo.Stack
SET     Telephone = STR(CONVERT(FLOAT, Telephone), 15, 0)
OUTPUT  deleted.Telephone, inserted.Telephone -- This clause will show affected phone numbers
WHERE   Telephone LIKE '%e%';

ROLLBACK;
-- COMMIT

Note: you could avoid these problems if you import phone numbers into a VARCHAR column directly and not into a FLOAT column. 注意:如果将电话​​号码直接导入VARCHAR列而不是FLOAT列,则可以避免这些问题。

3) Then you could use: 3)然后您可以使用:

BEGIN TRANSACTION;

UPDATE  dbo.Stack
SET Telephone = STUFF(Telephone, 1, 2, '0')
WHERE   Telephone LIKE '447%';

ROLLBACK;
-- COMMIT

Note: you could do this replace in Excel using, of course, Excel functions. 注意:您当然可以使用Excel函数在Excel中进行替换。

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

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