思念食品API

sql_GetPhoneLocation_20180517.sql 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /****** Object: UserDefinedFunction [dbo].[GetDeptName] Script Date: 05/17/2018 17:40:36 ******/
  2. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetPhoneLocation]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  3. DROP FUNCTION [dbo].[GetPhoneLocation]
  4. GO
  5. /****** Object: UserDefinedFunction [dbo].[GetDeptName] Script Date: 05/17/2018 17:40:36 ******/
  6. SET ANSI_NULLS ON
  7. GO
  8. SET QUOTED_IDENTIFIER ON
  9. GO
  10. CREATE FUNCTION [dbo].[GetPhoneLocation] (
  11. @Caller NVARCHAR(50)
  12. )
  13. RETURNS varchar(50)
  14. AS
  15. BEGIN
  16. declare @areacode nvarchar(50)--区号
  17. if LEN(@Caller)=11 and @Caller LIKE '[1][3456789]%'
  18. begin
  19. --处理主叫号码 获取手机号码前7位
  20. declare @Top7 char(10)
  21. set @Top7=SubString(@Caller, 1,7);
  22. --根据前7位获取区号
  23. select @areacode=F_ZipCode FROM T_Sys_MobileData where F_MobileNum=@Top7
  24. end
  25. else
  26. begin
  27. --固定电话获取归属地
  28. if LEN(@Caller)>10
  29. begin
  30. declare @three char(5)
  31. declare @four char(5)
  32. declare @cc int
  33. set @three=LEFT(@Caller, 3);
  34. set @four=LEFT(@Caller, 4);
  35. --根据区号获取地区ID
  36. select @cc=COUNT(1) FROM tel_location where tel=@four
  37. if @cc>0
  38. begin
  39. set @areacode=@four
  40. end
  41. else
  42. begin
  43. set @areacode= @three
  44. end
  45. end
  46. else
  47. begin --号码小于10位默认为本地固定电话,默认区号为0371
  48. set @areacode='0371'
  49. end
  50. end
  51. return @areacode
  52. END
  53. GO