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

MSSQL Format U.S. Phone Number

Posted on October 13, 2014October 26, 2015 By David Kittell
SELECT CASE LEN(phone)
		WHEN 11
			THEN LEFT(phone, 1) + STUFF(STUFF(STUFF(phone, 1, 1, ' ('), 6, 0, ') '), 11, 0, '-')
		WHEN 10
			THEN STUFF(STUFF(STUFF(phone, 1, 0, ' ('), 6, 0, ') '), 11, 0, '-')
		WHEN 7
			THEN STUFF(phone, 4, 0, '-')
		ELSE 'Invalid phone'
		END AS 'Formatted Phone'
FROM TABLE

If you have to sanitize your field first it gets ugly but is still possible

CASE LEN(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '.', ''), '-', ''), '(', ''), ')', ''), ' ', ''))
		WHEN 11
			THEN LEFT(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '.', ''), '-', ''), '(', ''), ')', ''), ' ', ''), 1) + STUFF(STUFF(STUFF(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '.', ''), '-', ''), '(', ''), ')', ''), ' ', ''), 1, 1, ' ('), 6, 0, ') '), 11, 0, '-')
		WHEN 10
			THEN STUFF(STUFF(STUFF(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '.', ''), '-', ''), '(', ''), ')', ''), ' ', ''), 1, 0, ' ('), 6, 0, ') '), 11, 0, '-')
		WHEN 7
			THEN STUFF(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '.', ''), '-', ''), '(', ''), ')', ''), ' ', ''), 4, 0, '-')
		ELSE 'Invalid phone'
		END AS Phone

Reference: http://www.sqlservercurry.com/2010/11/format-phone-numbers-in-sql-server.html

Originally Posted on October 13, 2014
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

Ektron Find Replace In Menu

Posted on October 23, 2013October 26, 2015

This script will rename menu link paths SET XACT_ABORT ON DECLARE @currval NVARCHAR(500) DECLARE @newval NVARCHAR(500) DECLARE @search NVARCHAR(500) DECLARE @replace VARCHAR(500) SET @search = ” –string to find SET @replace = ” –replacement string DECLARE @pos INT DECLARE @id BIGINT BEGIN TRAN DECLARE curs CURSOR LOCAL FAST_FORWARD FOR SELECT…

Read More

Ektron Alias With Folder Name

Posted on February 25, 2014October 26, 2015

With assistance from my Ektron Content Folder Names script this script will display all aliases with the folder that the content belongs to. This is not an ideal process as you can have many folders in Ektron but this can help. SET NOCOUNT ON DECLARE @Domain VARCHAR(50) SET @Domain =…

Read More

Delete Logs Older Than 6 Months

Posted on February 25, 2013October 26, 2015

DELETE FROM LogTable WHERE log_datetime < dateAdd(m, -6, getDate()) Originally Posted on February 25, 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 it is suggested you test…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

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
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes