Solved

How to replace WHITESPACE (1 or more spaces) in a field with a SINGLE DASH?

Posted on 2016-09-04
4
41 Views
Last Modified: 2016-09-04
My field has data like this:

1P0007.63579.0000                           1609168541          899972053           1230

I need to remove all spaces (1 or more) and replace the blank space with a single dash '-' like this:

1P0007.63579.0000-1609168541-899972053-1230

Can someone tell me what SQL Code to use?
0
Comment
Question by:MIKE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 41783844
Take a look here: http://sqlmag.com/t-sql/t-sql-string-manipulation-tips-and-techniques-part-1

Especially the paragraph about replacing any number of spaces with a single space. Just modify the last step to create the hyphen:

SELECT REPLACE(REPLACE(REPLACE(yourcolumn, ' ', '~ '), ' ~', ''), '~ ', '-') as newcolumn FROM yourtable

Open in new window


Bye, Olaf.
0
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 250 total points
ID: 41783849
From different sources, the method to use in SQL server is to use a triple replace. In the first replace, change all double spaces to two characters that are not used in your string, such as {}. In the second replace, change the inverted characters you used, }{, with nothing. In the third replace, change the remaining {} to your desired character. You may also need to insert another replace between the second and third to replace the odd space with nothing.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41783852
A similar method could be used with the scripting function in SSRS if you are generating a report.
0
 
LVL 17

Author Comment

by:MIKE
ID: 41783962
MANY, MANY THANKS!

Now please see my new question related to this...here:  
https://www.experts-exchange.com/questions/28967659/How-to-CONCANTENATE-ROWS-of-data-into-a-flat-single-ROW.html
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question