I have created small function using T-SQL which takes one input parameter as phone number and returns area code from it. Function compiles successfully but getting only first digit of the phone number instead of three digits.
Refer below code:-
create or alter function getareacode (@phoneno as nvarchar)
returns nvarchar(10)
with schemabinding
as
begin
declare @areacode as nvarchar(10);
select top(1) @areacode = value from string_split(@phoneno,'-');
return @areacode;
end;
select dbo.getareacode( N'123-456-789');
LEFT(@phoneno, PATINDEX('%-%', @phoneno)-1)can also get you everything before the first-