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
Solved

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

Posted on 2016-09-04
4
39 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 40
Database ERD 4 29
SQL Server / Update DB? 22 36
Acces SQL Insert Problem 6 31
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

860 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