Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ragh
Starting Member
34 Posts |
Posted - 2003-06-10 : 02:38:20
|
| Hi There !Can anyone help me?I have a column lastname having data with uneven spaces inbetween text, but the spaces inbetween are equal. Now i want to remove these uneven spaces and make only single spaces, how can i acheive this ?Example:--------Lastname---------SAM JACKSON MIKE (2 Spaces)SAM JACKSON MIKE (3 Spaces)SAM JACKSON MIKE (8 Spaces)Likewise i have many data stored in one column with spaces, now i want to truncate them to single space.please give me a query...regardsRaghRagh |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-06-10 : 03:31:14
|
You could write a few UPDATE statements that replace multiple spaces with a single space...or you could write a pretty script to do that DECLARE @NoOfSpaces TINYINTSET @NoOfSpaces = 10WHILE @NoOfSpaces > 1BEGIN UPDATE TableName SET LastName = REPLACE(LastName, SPACE(@NoOfSpaces), '') SET @NoOfSpaces = @NoOfSpaces - 1ENDI havent tested it, so better take a backup before you try this!Owais |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
|
|
ragh
Starting Member
34 Posts |
Posted - 2003-06-10 : 04:45:22
|
| Thanks guys, it works i just changed, ok some minor change thats itbut how can i implement this for SELECTing the rows without UPDATING...?DECLARE @NoOfSpaces TINYINT SET @NoOfSpaces = 20WHILE @NoOfSpaces >= 1 BEGIN UPDATE Pantable SET LastName = REPLACE(LastName, SPACE(@NoOfSpaces), ' ') SET @NoOfSpaces = @NoOfSpaces - 1 ENDRagh |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
|
|
ragh
Starting Member
34 Posts |
Posted - 2003-06-10 : 05:32:42
|
| Thanks Arnolddoes this works out...SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(lastname,REPLICATE(' ',21),' '),REPLICATE(' ', 6),' '),REPLICATE(' ', 3),' '),REPLICATE(' ', 2),' '),REPLICATE(' ', 2),' ')FROM PantableRagh |
 |
|
|
|
|
|
|
|