473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replacing repeating blanks with a single blank

hi all,

I have a scenario where in i need to replace repeating blanks with a single blank of a column in the database.

ex: ASD KL OP ---> ASD KL OP

any suggestions??

Regards,
Leela
Jan 3 '08 #1
9 1559
ck9663
2,878 Expert 2GB
hi all,

I have a scenario where in i need to replace repeating blanks with a single blank of a column in the database.

ex: ASD KL OP ---> ASD KL OP

any suggestions??

Regards,
Leela

the challenge: you don't know at any point in time how many spaces are there in between. this is string cleaning (like address), you might want to create a function that parse the string and remove repeating blanks. consideration: not because you can not see does not mean it's blank. there are "unprintable" characters.

-- CK
Jan 3 '08 #2
the challenge: you don't know at any point in time how many spaces are there in between. this is string cleaning (like address), you might want to create a function that parse the string and remove repeating blanks. consideration: not because you can not see does not mean it's blank. there are "unprintable" characters.

-- CK
but in my case i am sure that its a space but is there any one liner function which can do this trick?????
Jan 4 '08 #3
ck9663
2,878 Expert 2GB
but in my case i am sure that its a space but is there any one liner function which can do this trick?????
is this a one time project? how many fields are you trying to clean up?

-- CK
Jan 4 '08 #4
is this a one time project? how many fields are you trying to clean up?

-- CK
its not a project or something, there is one column in the database which has spaces in between and needs to be removed.
Jan 4 '08 #5
ck9663
2,878 Expert 2GB
its not a project or something, there is one column in the database which has spaces in between and needs to be removed.

if it's a one-time, manual kind of task try:

Expand|Select|Wrap|Line Numbers
  1. update yourtable
  2. set yourfield = replace(replace(replace(yourfield,'  ',' '),'  ',' '),'  ',' ')
  3. where patindex('%  %',yourfield) > 0
  4.  
you're going to have to run this a number of times, until it no longer returns any resultset. it will just replace any 2 consecutive space with 1 space. so, if there are 3 spaces in between string, it will be reduced to 2, but the second replace will reduce it further to 1. if there are 4 it will reduce to 2, and so on...

you can try this first:

Expand|Select|Wrap|Line Numbers
  1. select yourfield, replace(replace(replace(yourfield,'  ',' '),'  ',' '),'  ',' ')
  2. from yourtable
  3. where patindex('%  %',yourfield) > 0
  4.  

this will give you an idea if you're in the right direction

-- CK
Jan 4 '08 #6
thanks for ur info... :)
this i have tried i wanted some other better approach than this if any........
Jan 4 '08 #7
ck9663
2,878 Expert 2GB
create a string parser function...


-- CK
Jan 5 '08 #8
hailua
4
Create FUNCTION [dbo].[replace_blank]
( @input nvarchar(max))
RETURNS nvarchar(4000) As
Begin
Declare @output nvarchar(4000)
SET @input = ltrim(rtrim(@input))
SET @output = ''

While 1=1
Begin
IF PATINDEX('% %',@input) = 0 BREAK
SET @output = ltrim(rtrim(@output))+' '+ltrim(rtrim(SubString(@input,1,CharIndex(' ',@input))))
SET @input = SubString(@input,CharIndex(' ',@input)+1,Len(@input))
End
SET @output = Left(@output,Len(@output))+' '+@input
Return @output
End

-- select dbo.replace_blank('hello: is this the one you looking at?. ')
Jan 7 '08 #9
hailua
4
Or you can use
-----------------------------
Create FUNCTION [dbo].[replace_blank]
( @input nvarchar(4000))
RETURNS nvarchar(4000) As
Begin
While 1=1
Begin
IF PATINDEX('% %',@input) = 0 BREAK
SET @input = replace(@input,' ',' ')
End
SET @input = ltrim(rtrim(@input))
Return @input
End
-------------------------------
-- select dbo.replace_blank('hello is this the one you looking at. ')
Jan 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Moon | last post by:
Seems I still haven't got the hang of all those window generating code in Javascript. I've got a page with about 15 photo thumbnails. When you click on a thumbnail a new window pops up which shows...
2
by: William Kossack | last post by:
I have an Access table with one primary key and am attempting to update a non-key field, using UPDATE tblMethtest SET fev1timemeth = '' WHERE SID = '0041R'; When I do this, the field...
11
by: Christoph Boget | last post by:
When building a form using Infopath, you can define a repeating section and stick form fields in that section. I'm curious if ASP.NET has a similar control to make it easy to design something...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
8
by: wyo | last post by:
I'd like to display folder names without line breaks so I tried the following replace folders = f.replace(/ /g, '&nbsp;'); but the script stop without an error. Any idea? Is there a better...
10
by: Diego F. | last post by:
Hi all. I have an application that receives a message from a socket in an array from a certain size. As the array size may be longer that the message received, the end of the array has blank...
1
by: Ronald S. Cook | last post by:
I have a DataTable with column "Sequence". Values are like: Sequence -------- 5 1 3 (blank/null) 7 6
0
by: dhascuba | last post by:
The @name field can contain a single quote in it such as: Mike O'Grady. Since this is creating and SQL statement on the server side, it will not process the name if it has a single quote in it. Id'...
4
by: eBob.com | last post by:
I have a RichTextBox in which I'd like blanks to appear different from nothing. Imagine a file which does not fill up the RTB. You can't tell how many, if any, blanks might follow the last...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.