
[dbo].[GetCustomerPhone1AreaCodeLocalNbrExt_XLedger]
CREATE PROCEDURE [dbo].[GetCustomerPhone1AreaCodeLocalNbrExt_XLedger]
@inCustomerNumber varchar(15),
@inCustomerAddressCode varchar(15)
as
set nocount on
declare @thePhone varchar(30)
declare @theAreaCode PhoneAreaCode
declare @theLocalNumber PhoneLocalNumber
declare @thePhoneExtension PhoneExtension
declare @theIndex int
declare @theIndex2 int
select
@thePhone = rtrim( ltrim( AccountingCustomer.Phone1 ) )
from AccountingCustomer
where AccountingCustomer.CustomerNumber = @inCustomerNumber
if( @thePhone != '' )
begin
set @theIndex = charindex( 'ext', @thePhone )
if( @theIndex > 7 )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhone, @theIndex + 3, 30 ) ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, 1, @theIndex - 1 ) ) )
end
else
begin
set @theIndex = charindex( 'ex', @thePhone )
if( @theIndex > 7 )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhone, @theIndex + 2, 30 ) ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, 1, @theIndex - 1 ) ) )
end
else
begin
set @theIndex = charindex( 'x', @thePhone )
if( @theIndex > 7 )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhone, @theIndex + 1, 30 ) ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, 1, @theIndex - 1 ) ) )
end
end
end
if( len( @thePhone ) = 10 )
begin
set @theAreaCode = substring( @thePhone, 1, 3 )
set @theLocalNumber = concat( substring( @thePhone, 4, 3 ), '-', substring( @thePhone, 7, 4 ) )
set @thePhone = ''
end
else if( len( @thePhone ) = 7 )
begin
set @theAreaCode = null
set @theLocalNumber = concat( substring( @thePhone, 1, 3 ), '-', substring( @thePhone, 4, 4 ) )
set @thePhone = ''
end
else if( len( @thePhone ) = 8 and substring( @thePhone, 4, 1 ) = '-' )
begin
set @theAreaCode = null
set @theLocalNumber = @thePhone
set @thePhone = ''
end
else if( len( @thePhone ) >= 12 and substring( @thePhone, 4, 1 ) = '-' and substring( @thePhone, 8, 1 ) = '-' )
begin
set @theAreaCode = substring( @thePhone, 1, 3 )
set @theLocalNumber = substring( @thePhone, 5, 8 )
set @thePhone = ltrim( rtrim( substring( @thePhone, 13, 30 ) ) )
end
else
begin
set @theIndex = charindex( '(', @thePhone )
set @theIndex2 = charindex( ')', @thePhone )
if( @theIndex > 0 and @theIndex2 > @theIndex )
begin
set @theAreaCode = ltrim( rtrim( substring( @thePhone, @theIndex + 1, @theIndex2 - @theIndex - 1 ) ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, @theIndex2 + 1, 30 ) ) )
end
else
begin
set @theIndex = charindex( '-', @thePhone )
set @theIndex2 = charindex( '-', @thePhone, @theIndex + 1 )
if( @theIndex > 0 and @theIndex2 > @theIndex )
begin
set @theAreaCode = ltrim( rtrim( substring( @thePhone, 1, @theIndex - 1 ) ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, @theIndex + 1, 30 ) ) )
end
end
if( len( @thePhone ) >= 8 and substring( @thePhone, 4, 1 ) = '-' )
begin
set @theLocalNumber = substring( @thePhone, 1, 8 )
set @thePhone = ltrim( rtrim( substring( @thePhone, 9, 30 ) ) )
end
else if( len( @thePhone ) >= 7 )
begin
set @theLocalNumber = concat( substring( @thePhone, 1, 3 ), '-', substring( @thePhone, 4, 4 ) )
set @thePhone = ltrim( rtrim( substring( @thePhone, 8, 30 ) ) )
end
end
if( isnull( @theLocalNumber, '' ) = '' )
begin
set @theLocalNumber = @thePhone
end
else if( isnull( @thePhoneExtension, '' ) = '' )
begin
set @thePhoneExtension = @thePhone
end
if( isnull( @thePhoneExtension, '' ) != '' )
begin
if( substring( @thePhoneExtension, 1, 1 ) = '.' )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhoneExtension, 2, 30 ) ) )
end
if( substring( @thePhoneExtension, 1, 1 ) = 'x' )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhoneExtension, 2, 30 ) ) )
end
if( substring( @thePhoneExtension, 1, 1 ) = 'x' )
begin
set @thePhoneExtension = ltrim( rtrim( substring( @thePhoneExtension, 2, 30 ) ) )
end
end
set @thePhoneExtension = ltrim( rtrim( case
when isnull( @thePhoneExtension, '' ) = '' then null
when substring( @thePhoneExtension, 1, 1 ) = 'x' then substring( @thePhoneExtension, 2, 30 )
else @thePhoneExtension
end ) )
end
select
AreaCode = @theAreaCode,
LocalNumber = @theLocalNumber,
Extension = @thePhoneExtension
GO
GRANT EXECUTE ON [dbo].[GetCustomerPhone1AreaCodeLocalNbrExt_XLedger] TO [MssExec]
GO