我在数据库中有电话号码字段。它已有数据。
我想将手机号码格式更改为“XXX-XXX-XXXX” 当前数据库没有任何电话格式。
因此可能存在垃圾数据。我已经对新记录应用了验证,但现在我也要更改现有数据。
我是否有任何具体方法可以更改现有数据。并使所有电话号码都遵循这种格式。
请建议。
答案 0 :(得分:2)
创建功能以删除非数字数据并进行格式化
CREATE FUNCTION [UDF_STRIP_NONNUMERIC_DATA](@str VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
WHILE Patindex('%[^0-9]%', @str) > 0
BEGIN
SET @str = Stuff(@str, Patindex('%[^0-9]%', @str), 1, '')
END
RETURN @str
END
您可以使用STUFF
功能在-
号码之间插入phone
Select left(Stuff(Stuff(dbo.[UDF_STRIP_NONNUMERIC_DATA](Phone),4,0,'-'),8,0,'-'),12)
From yourtable
如果您使用的是SQL SERVER 2012+
,可以使用FORMAT
功能(感谢LukStorms,他在评论中提到了它)
SELECT Format(Cast(dbo.[Udf_strip_nonnumeric_data](Phone) AS BIGINT), '###-###-####')
FROM yourtable
更新
Update yourtable
SET phone = left(Stuff(Stuff(dbo.[UDF_STRIP_NONNUMERIC_DATA](Phone),4,0,'-'),8,0,'-'),12)
<强>演示强>
declare @str varchar(100)= '9225-123-4567'
select left(Stuff(Stuff(dbo.[UDF_STRIP_NONNUMERIC_DATA](@str),4,0,'-'),8,0,'-'),12)
结果: 922-512-3456
答案 1 :(得分:1)
public ArrayList<Invoice> getInvoices() {
readPersons();
readCustomers();
readProducts();
Scanner sc = null;
try {
sc = new Scanner(new File("data/Invoices.dat"));
sc.nextLine();
while (sc.hasNextLine()) {
ArrayList<Product> product = new ArrayList<Product>();
String line = sc.nextLine();
String data[] = line.split(";");
String invoiceCode = data[0].trim();
String customerCode = data[1].trim();
Customer customer = null;
for(Customer aCustomer: customerList) {
if (customerCode.equals(aCustomer.getCustomerCode())) {
customer = aCustomer;
break;
}
}
String personCode = data[2].trim();
Person person = null;
for(Person aPerson: personList) {
if (personCode.equals(aPerson.getPersonCode())) {
person = aPerson;
break;
}
}
String invoiceDate = data[3];
String products[] = data[4].split(",");
for (int i = 0; i < products.length; i++) {
String productData[] = products[i].split(":");
for(Product aProduct: productList) {
if (aProduct.getProductCode().equals(productData[0])) {
aProduct.setInvoiceDate(this.getDateTime(invoiceDate));
if (productData.length == 1) {
aProduct.setQuantity(1);
product.add(aProduct);
} else if (productData.length == 2) {
aProduct.setQuantity(Integer.parseInt(productData[1]));
product.add(aProduct);
} else if (productData.length == 3) {
aProduct.setQuantity(Integer.parseInt(productData[1]));
for(Product anotherProduct: product) {
if (anotherProduct.getProductCode() == productData[2]) {
aProduct.setParkingPassCount(anotherProduct.getQuantity());
break;
}
}
product.add(aProduct);
}
break;
}
}
}
// Creates an Invoice object
Invoice invoice = new Invoice(invoiceCode, invoiceDate, customer, person, product);
// Adds the Invoice object into Invoice ArrayList
invoiceList.add(invoice);
}
sc.close();
return invoiceList;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
public DateTime getDateTime(String Date){
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dateTime = formatter.parseDateTime(Date);
return dateTime;
}
答案 2 :(得分:0)
好的,要替换所有非数字字符,请查看this。
以下是一个示例脚本(从该链接复制)以向您展示它是如何工作的(您需要修改它以适合您的表名和列名:
-- Step 1: creates table to use to hold every char in every phone number
if object_id('dbo.tally') is not null drop table dbo.tally
select top 10000 --change to fit max length of phone number
identity(int,1,1) as n
into dbo.tally
from master.dbo.syscolumns sc1,
master.dbo.syscolumns sc2
-- add pk to maximize performance
alter table dbo.tally
add constraint pk_tally_n
primary key clustered (n) with fillfactor = 100
-- Step 2: Create temporary table holding three bad phone numbers
declare @phonetable table
(uniqueid int identity(1,1),
phone_number varchar(500))
insert into @phonetable (phone_number)
select '01234-567-890' union
select '012345 6789ext' union
select 'n/a' union select '...12345.....';
-- Step 3: identify, for every character, whether it is a number or not,
and remove the non-numeric ones
with cte (uniqueid, phone_number, goodchar, badchar) as
( select uniqueid, phone_number,
case when substring(phone_number,N,1) not like '%[^0-9]%'
then substring(phone_number,N,1) end as goodchar,
case when substring(phone_number,N,1) like '%[^0-9]%'
then substring(phone_number,N,1) end as badchar
from @phonetable , Tally
where phone_number like '%[^0-9]%' and N <= len(phone_number) )
select distinct phone_number,
isnull( stuff (
( SELECT '' + goodchar
FROM cte t1
where t1.UniqueID = t2.UniqueID
FOR XML PATH ( '' ) ) , 1 , 0 , '' ) ,'')
as clean_phone_number from cte t2
显示带格式的数字,只需提取相应的部分,然后用破折号重新连接它们。
Select case len(phone)
When 10 then left(phone, 3) + '-' +
substring(phone, 4,3) + '-' +
substring(phone, 7,4)`
When 7 then left(phone, 3) + '-' +
substring(phone, 4,4)
Else '' end
创建计算列
Alter table Add Column FormattedPhone as
case len(phone)
When 10 then left(phone, 3) + '-' +
substring(phone, 4,3) + '-' +
substring(phone, 7,4)`
When 7 then left(phone, 3) + '-' +
substring(phone, 4,4)
Else '' end
答案 3 :(得分:0)
如果你不介意UDF
dict
返回
Select [dbo].[udf-Str-Format-Phone]('334)789-4532')
UDF
334-789-4532