Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Extracting Numeric values from a varchar field

Extracting Numeric values from a varchar field

Scheduled Pinned Locked Moved Database
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Wimsatt
    wrote on last edited by
    #1

    I'm trying to standardize the phone number format for some historical records. The phone numbers have been entered in different formats in the past. I would like to extract just the numerical characters from the old phone numbers so I can reformat them into an xxx.xxx.xxxx format. I've looked into using the Replace and Substring functions, but I was hoping an easier solution existed.


    A 1 Reply Last reply
    0
    • A Adam Wimsatt

      I'm trying to standardize the phone number format for some historical records. The phone numbers have been entered in different formats in the past. I would like to extract just the numerical characters from the old phone numbers so I can reformat them into an xxx.xxx.xxxx format. I've looked into using the Replace and Substring functions, but I was hoping an easier solution existed.


      A Offline
      A Offline
      Adam Wimsatt
      wrote on last edited by
      #2

      I found a solution using code provided on this site: http://www.vsj.co.uk/articles/display.asp?id=540[^] Here is a Scalar-valued SQL Function to do the job: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION RemoveNonNumerics ( -- Add the parameters for the function here @sInputValue varchar(24) ) RETURNS varchar(24) AS BEGIN -- Declare the return variable here DECLARE @Result varchar(24) -- Add the T-SQL statements to compute the return value here SELECT @Result = @sInputValue -- Remove non-numeric characters declare @i INT select @i = 25 while @i > 0 begin SELECT @i = patindex('%[^0-9]%',@Result) SELECT @Result = replace(@Result,substring(@Result,@i,1),'') end -- Return the result of the function RETURN @Result END GO


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups