Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 395 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Removing Space from string

Hi All ,
I was looking for a help
I have a table city
in this table there is column "City_name"
that contains value like
City_name
RUTLAND |
Pendleton |
SIERRA |
RANDOLPH |
YANCEY |
PATRICK |
DENVER |
CALDWELL |
SCOTLAND |
KEARNEY |
PASCO |
CATAWBA |
LINN |
FORT BEND |
COLLIN |
RIO ARRIBA |
Most of these values contains spaces & New line character
to remove those i used upper(REGEXP_REPLACE(city_name, '[^0-9A-Za-z]', ''))
this worked for every city except where city name like "RIO ARRIBA" this removed the space between RIO and ARRIBA .
is there any way by which we could removed space, and new line character from the last and starting only if there is a space between string that should remain same..
thanks in advance
Answers
-
Use TRIM, LTRIM, RTRIM and REPLACE (if needed).
EDIT: Here are the links for you from documentation, there are examples also, so you can try yourself.
-
So you want to remove trailing spaces and new line characters, right? If so, use
RTRIM("City_Name",' ' || CHR(10) || CHR(13))
SY.
-
Hi,
So, the sub-string returned must start and end with an alphanumeric character, is that right?
Here's one way:
REGEXP_SUBSTR ( city_name , '[[:alnum:]]([[:alnum:] ]*[[:alnum:]])?' )
I hope this answers you question.
If not, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 12.1.0.2.0).
-
If you want to remove spaces, carriage returns and line feeds from the data, whether they are leading or trailing, you can use this :
LTRIM(RTRIM("City_Name", ' ' || CHR(13) || CHR(10)), ' ' || CHR(13) || CHR(10))
This will perform better than a regex solution.
-
this will not work for the city names those have two words "New York" this will removed the space from these two words as well .. that should not be happn
-
sandeepgupta18 wrote:this will not work for the city names those have two words "New York" this will removed the space from these two words as well .. that should not be happn
Did you try it? It doesn't remove "in-between" spaces:
-- Added enclosing brackest to show trailing spaces and new line characters are indeed removed
SELECT '[' || REGEXP_SUBSTR('New York ' || CHR(10) || ' ','[[:alnum:]]([[:alnum:] ]*[[:alnum:]])?') || ']'
FROM DUAL
/
'['||REGEX
----------
[New York]
SQL>
But as I already stated, same can be done more efficiently using RTRIM:
-- Added enclosing brackest to show trailing spaces and new line characters are indeed removed
SELECT '[' || RTRIM('New York ' || CHR(10) || ' ',' ' || CHR(10) || CHR(13)) || ']'
FROM DUAL
/
'['||RTRIM
----------
[New York]
SQL>
SY.
-
Hi,
sandeepgupta18 wrote:this will not work for the city names those have two words "New York" this will removed the space from these two words as well .. that should not be happn
No, the expression in reply #3 will only remove spaces at the beginning or end or the string. It will not remove spaces between alphanumerics.
Maybe you copied it wrong. Post your query.
-
Sorry it was my mistake i was checking wrong values but yes it worked okay...
but can you please explain the query and how it is working cause i am not good with playing regexp
SELECT '[' || REGEXP_SUBSTR('New York ' || CHR(10) || ' ','[[:alnum:]]([[:alnum:] ]*[[:alnum:]])?') || ']'
FROM DUAL
-
can you please explain the query and how it is working cause i am not good with playing regexp
REGEXP_SUBSTR ( city_name
, '[[:alnum:]]([[:alnum:] ]*[[:alnum:]])?'
)
-
Hello Sandeep,
<<can you please explain the query and how it is working cause i am not good with playing regexp>>
;-) so it takes a long time to understand today, next DBAs looking at the code tomorrow and later will also spend time on the same... And the solution with TRIM is simple to understand and short to write. What is the conclusion? Do we need the power or REGEXP? You can use a canon to kill a fly... if you want, but maybe it is not necessary?
Edited: added: and test the various options with city names like <Mantes-la-Jolie> <Paris (Texas)> <Westward Ho!> <Saint-Louis-du-Ha! Ha!>
Amazed regards,
Bruno Vroman.