我有3个临时表,它们为移动电话号码创建两列;
例如,手机1具有所有国家和国际号码,而手机2具有少数需要从++41
转换为0041
的数字。
如何将这些数字合并为一列,从而使我保留了mobile1的所有内容,除了少数更改的++数字
它看起来像:
mobile 1 cust mobile 2
-------------------------------------------
078998899 98743872
079445544 12342343
+637976945247 41768999 00637976945247
+41405355893 98767856 0041405355893
+41412387946 43789347 0041412387946
+4113120464 85903479 000413120464
应该是:
mobile final cust
----------------------------------
078998899 98743872
079445544 12342343
00637976945247 41768999
0041405355893 98767856
0041412387946 43789347
000413120464 85903479
我在这里尝试过,但是对括号并不满意,在此我会很乐意提供帮助。这是用于SQL Server
SELECT *
INTO #temp_UNION
FROM
(SELECT *
FROM [#national_numbers]
UNION
SELECT *
FROM [#international_numbers]
UNION
SELECT *
FROM [#plusplus_numbers]
UNION) v
答案 0 :(得分:0)
借助CASE WHEN和LEFT,CHARINDEX,SUBSTRING和Like,可以根据条件将两列数据设置为一列。
我强烈建议使用CHARINDEX以获得更好的性能,Here are the results, in milli-seconds, in our LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX speed tests.
create table #Test (mobile1 varchar(100),cust varchar(100),mobile2 varchar(100))
insert into #Test values('078998899','98743872',null)
insert into #Test values('079445544','12342343',null)
insert into #Test values('+637976945247','41768999','00637976945247')
insert into #Test values('+41405355893','98767856','0041405355893')
insert into #Test values('+41412387946','43789347','0041412387946')
insert into #Test values('+4113120464','85903479','000413120464')
使用CharIndex:
SELECT CASE WHEN CHARINDEX ('+',mobile1)=1 THEN mobile2 ELSE mobile1 END As MobileNo, cust from #Test