• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

SQL Truncate

Hello.

I need to trim a column of phone numbers to remove spaces and truncate to 20 numbers.

Number range includes.

xxx xxxxxxxxx
xxxxxxxxxxxxx
xxxxx xxxxxxx
xxxx xxxx xxx

thanks.
0
aneilg
Asked:
aneilg
  • 2
2 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
What is the data type of the field?
Which spaces do you want to remove (left, right, middle)?
Can you give us an example of current data and the expected results?
0
 
aneilgAuthor Commented:
Hello.

Data Type Varchar(23)

Its to remove any spaces in the phone field.

Now             xxx xxxxxx
Result          xxxxxxxxx

Now             xxxx xxxxx
Result          xxxxxxxxx

thanks.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can use the REPLACE function:
SELECT REPLACE(PhoneNumberFieldName, ' ', '')

Open in new window

0
 
Scott PletcherSenior DBACommented:
SELECT LEFT(REPLACE(PhoneNumberFieldName, ' ', ''), 20) --and truncate to 20 "numbers"

Q: Do you need/want to check for specifically "numbers" in the column?  Do you want to strip all non-numerics?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now