Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Format US Phone

Posted on August 6, 2013October 26, 2015 By David Kittell

Worst case scenario, for when you do not have the ability to do Regex replace.

SELECT dbo.FormatUSPhone('989.989.9898') AS Phone
	,dbo.FormatUSPhone('(989)989.9898') AS Phone2
	,dbo.FormatUSPhone('(989) 989-9898') AS Phone3
	,dbo.FormatUSPhone('(989) 989-989') AS Phone4
	,dbo.FormatUSPhone('989/989/9898') AS Phone5
	,dbo.FormatUSPhone('9899899898') AS Phone6
Phone                     Phone2                    Phone3                    Phone4                    Phone5                    Phone6
------------------------- ------------------------- ------------------------- ------------------------- ------------------------- -------------------------
(989) 989-9898            (989) 989-9898            (989) 989-9898            989989989                 (989) 989-9898            (989) 989-9898
IF OBJECT_ID(N'dbo.FormatUSPhone') IS NOT NULL
	DROP FUNCTION dbo.FormatUSPhone
GO

CREATE FUNCTION dbo.FormatUSPhone (@string VARCHAR(250))
RETURNS VARCHAR(25)
AS
BEGIN
	DECLARE @Formatted VARCHAR(25)

	SET @string = ltrim(rtrim(Replace(Replace(Replace(Replace(Replace(Replace(Replace(@string, ')', ''), '(', ''), '.', ''), ' ', ''), '-', ''), '/', ''), '', '')))
	SET @Formatted = (
			CASE
				WHEN len(@string) <> '10'
					THEN @string
				WHEN len(@string) = '10'
					THEN '(' + SUBSTRING(@string, 1, 3) + ')' + ' ' + SUBSTRING(@string, 4, 3) + '-' + SUBSTRING(@string, 7, 4)
				WHEN @string = ''
					THEN ' '
				END
			)

	RETURN @Formatted
END
GO

I have modified the code from the source link below to account for incorrect/invalid phone numbers. The original code will display NULL if the phone number is not valid where my edit will show the character stripped original string.

Source: http://nice-tutorials.blogspot.com/2009/04/how-to-format-phone-number-in-ms-sql.html

Originally Posted on August 6, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL

Post navigation

Previous post
Next post

Related Posts

MSSQL – Datetime In Local Timezone

Posted on February 2, 2017March 26, 2021

This function will get the local time based on the time zone difference from UTC IF OBJECT_ID(N’[GetLocalDate]’) IS NOT NULL DROP FUNCTION [GetLocalDate] GO CREATE FUNCTION [dbo].[GetLocalDate] (@TimezoneDiffInHour FLOAT = – 5) RETURNS DATETIME AS BEGIN RETURN DATEADD(Hh, @TimezoneDiffInHour, GETUTCDATE()) END; SELECT dbo.GetLocalDate(DEFAULT); — or SELECT dbo.GetLocalDate(-5); SELECT dbo.GetLocalDate(+1); Originally…

Read More

Mac OSX Terminal – Xcode Reset

Posted on December 5, 2016December 13, 2016

Make sure Xcode is closed. defaults delete com.apple.dt.Xcode rm -rfv ~/Library/Application\ Support/Developer/Shared/Xcode rm -rfv ~/Library/Saved\ Application\ State/com.apple.dt.Xcode.savedState rm -rfv ~/Library/Preferences/com.apple.dt.Xcode.* rm -rfv ~/Library/Preferences/com.apple.dt.xcodebuild.plist rm -rfv ~/Library/MobileDevice/Provisioning\ Profiles/* Originally Posted on December 5, 2016Last Updated on December 13, 2016 All information on this site is shared with the intention to help….

Read More

Ping Trace

Posted on July 18, 2013October 26, 2015

Set objShell = CreateObject("WScript.Shell") objShell.Run "%COMSPEC% /k ping 8.8.8.8",,True objShell.Run "%COMSPEC% /k tracert 8.8.8.8" Originally Posted on July 18, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes