且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server中使用循环来通过调用no来获取值。和国家

更新时间:2022-11-27 23:18:25

你的尝试有问题​​ - 被叫号码必须是一个varchar列,所以你可以使用SUBSTRING,但你没有用引号包围数字。



我'我们假设以下数据模式:

You have a problem with your attempt - the called Number must be a varchar column so you can use SUBSTRING but you haven't surrounded the number with quotes.

I've assumed the following data schemas:
create table [Call Details]
(
	OrigParty1 bigint,
	calledNo varchar(50),
	DateOfCall Date,
	seconds int
)
create table Country 
(
	id int,
	Country varchar(30),
	CountryCode int, 
	RatePerMin decimal(15,2)
)





此查询将获取每个详细信息致电:



And this query will get the details for each call:

select OrigParty1, CalledNo, Country, seconds * RatePerMin as TotalCost 
from [Call Details] CD
LEFT OUTER JOIN Country C 
   ON CD.calledNo LIKE '00' + CAST(C.CountryCode AS varchar) + '%'



注意会有问题,如果有的话2位数代码以与3位代码相同的两位数字开头。我不认为这可能是真正的国家代码的情况,它只是因为我使用了补充数据而变得明显。



- 我注意到了要拨打电话号码的另一个问题,您应首先使用国际拨号代码,然后是国家代码,后跟号码。我在这里介绍的解决方案不能处理这种情况 - 你需要用适当的代码替换硬编码的'00'


Note there will be an issue if any of the 2 digit codes start with the same two digits as a 3-digit code. I don't think this can be the case with genuine country codes, it only became apparent because I was using made up data.

- I have noticed another issue in that to call a number you should first use the international dialling code, followed by the country code followed by the number. The solution I've presented here doesn't handle that situation - you would need to replace the hard-coded '00' with the appropriate code