SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    format text field in sql

    I'd like to format a text field into a phone number format.
    For example:

    SQL currently spits out 4444444444
    I need it to output (444) 444-4444

    I've used the date_format but don't know of one for dealing with text.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select textphoneno
         , concat( '('
                 , substring(textphoneno from 1 for 3)
                 , ') '
                 , substring(textphoneno from 4 for 3)
                 , '-'
                 , substring(textphoneno from 7 for 4)
                 )  as formattedphoneno
      from yourtable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help!


    Quote Originally Posted by r937
    Code:
    select textphoneno
         , concat( '('
                 , substring(textphoneno from 1 for 3)
                 , ') '
                 , substring(textphoneno from 4 for 3)
                 , '-'
                 , substring(textphoneno from 7 for 4)
                 )  as formattedphoneno
      from yourtable


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •