How could I remove unnecessary characters in SQL - sql

I need a query that could remove unnecessary characters (a not-so-needed trailing comma as an example) from the string stored in my database table.
So that
EMAIL_ADD
abc#gmail.com,
abc#yahoo.com,def#example.org,
abs-def#ac.uk,
would update it into something like this:
EMAIL_ADD
abc#gmail.com
abc#yahoo.com,def#example.org
abs-def#ac.uk

Using TRIM() function with TRAILING option removes a specific unwanted character from end of string , in your case being a comma present at end.
UPDATE tableName
SET EMAIL_ADD = TRIM(TRAILING ',' FROM EMAIL_ADD)
See documentation here TRIM()

If you have a specific list of characters to filter out at the start and end use trim functions:
select ltrim(ltrim(rtrim(rtrim(email_add, ','), ' '), ','), ' ')
from tableX
Here I nested ltrim and rtrim to remove leading and trailing , and .
Or using trim:
select trim(trim(both ',' from email_add))
from tableX

if you only whant to remove the last character of a string you can use
update mytable set my_column = substr(my_column ,0,len(trim(my_column)-1) where mycolumn like '%,'
It is an untested example.

Related

Oracle remove special characters

I have a column in a table ident_nums that contains different types of ids. I need to remove special characters(e.g. [.,/#&$-]) from that column and replace them with space; however, if the special characters are found at the beginning of the string, I need to remove it without placing a space. I tried to do it in steps; first, I removed the special characters and replaced them with space (I used
REGEXP_REPLACE) then found the records that contain spaces at the beginning of the string and tried to use the TRIM function to remove the white space, but for some reason is not working that.
Here is what I have done
Select regexp_replace(id_num, '[:(),./#*&-]', ' ') from ident_nums
This part works for me, I remove all the unwanted characters from the column, however, if the string in the column starts with a character I don't want to have space in there, I would like to remove just the character, so I tried to use the built-in function TRIM.
update ident_nums
set id_num = TRIM(id_num)
I'm getting an error ORA-01407: can't update ident_nums.id_num to NULL
Any ideas what I am doing wrong here?
It does work if I add a where clause,
update ident_nums
set id_num = TRIM(id_num) where id = 123;
but I need to update all the rows with the white space at the beginning of the string.
Any suggestions are welcome.
Or if it can be done better.
The table has millions of records.
Thank you
Regexp can be slow sometimes so if you can do it by using built-in functions - consider it.
As #Abra suggested TRIM and TRANSLATE is a good choice, but maybe you would prefer LTRIM - removes only leading spaces from string (TRIM removes both - leading and trailing character ). If you want to remove "space" you can ommit defining the trim character parameter, space is default.
select
ltrim(translate('#kdjdj:', '[:(),./#*&-]', ' '))
from dual;
select
ltrim(translate(orginal_string, 'special_characters_to_remove', ' '))
from dual;
Combination of Oracle built-in functions TRANSLATE and TRIM worked for me.
select trim(' ' from translate('#$one,$2-zero...', '#$,-.',' ')) as RESULT
from DUAL
Refer to this dbfiddle
I think trim() is the key, but if you want to keep only alpha numerics, digits, and spaces, then:
select trim(' ' from regexp_replace(col, '[^a-zA-Z0-9 ]', ' ', 1, 0))
regexp_replace() makes it possible to specify only the characters you want to keep, which could be convenient.
Thanks, everyone, It this query worked for me
update update ident_nums
set id_num = LTRIM(REGEXP_REPLACE(id_num, '[:space:]+', ' ')
where REGEXP_LIKE(id_num, '^[ ?]')
this should work for you.
SELECT id_num, length(id_num) length_old, NEW_ID_NUM, length(NEW_ID_NUM) len_NEW_ID_NUM, ltrim(NEW_ID_NUM), length(ltrim(NEW_ID_NUM)) length_after_ltrim
FROM (
SELECT id_num, regexp_replace(id_num, '[:(),./#*&-#]', ' ') NEW_ID_NUM FROM
(
SELECT '1234$%45' as id_num from dual UNION
SELECT '#SHARMA' as id_num from dual UNION
SELECT 'JACK TEST' as id_num from dual UNION
SELECT 'XYZ#$' as id_num from dual UNION
SELECT '#ABCDE()' as id_num from dual -- THe 1st character is space
)
)

Replace function combined with ltrim and rtrim

Can someone please help me to understand the following code:
REPLACE(LTRIM(RTRIM(dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ','))), '.', '') AS BuildRequestID,
Does it say remove all trailing and leading spaces, then replace 3 with comma. Next, if there is ., replace it with ' '?
It does not at any point replace 3 with ,.
We can make all this easier to follow by formatting the full expression to cover multiple lines:
REPLACE(
LTRIM(RTRIM(
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
))
,'.', ''
) AS BuildRequestID,
Expressions like this have to read from the inside out. So we start with this inner-most part:
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
This UFN_SEPARATES_COLUMNS() function is not part of Sql Server, but was added by someone at your organization or as part of the vendor software package for the database you're looking at. But I'm confident based on inferences and the link (found via Google) it will treat CompletionDetails as delimited text, where the delimiter is a comma (based on the 3rd ',' argument) and returns the 3rd field (based on the 2nd 3 argument, where counting starts at 1 rather than 0). As CSV parsers go, this one is particularly naive, so be very careful what you expect from it.
Then we use LTRIM() and RTRIM() to remove both leading and trailing blanks from the field. Not all whitepsace is removed; only space characters. Tabs, line feeds, etc are not trimmed. Sql Server 2017 has a new TRIM() function that can handle wider character sets and do both sides of the string with one call.
The code then uses the REPLACE() function to remove all . characters from the result (replaces them with an empty string).
The code is trimming the leading and trailing spaces via the LTRIM() and RTRIM() functions of whatever is returned from the function dbo.x_COLUMNS... i.e. dbo.x_COLUMNS(CompletionDetails, 3, ','). LTRIM is left, RTRIM is right.
It then is replacing all periods (.) with nothing via the REPLACE() function.
So in summary, it's removing all periods from the string and the leading and trailing spaces.
The LTRIM removes leading spaces. RTRIM removes trailing spaces. REPLACE removes the period.
Declare #Val Char(20) = ' Frid.ay '
Select REPLACE(
LTRIM(
RTRIM(
#Val --dbo.x_COLUMNS(CompletionDetails, 3, ',')
)
), '.', ''
)
Result
BuildRequestID
--------------
Friday
remove all trailing and leading spaces, then replace 3 with comma.
Next, if there is ., replace it with ' '
No it does not say that.
But this does:
REPLACE(REPLACE(LTRIM(RTRIM(CompletionDetails)), '3', ','), '.', ' ')
it's not clear if you want . replaced by ' ' or ''.
I used the 1st case, you can change it as you like.
It's easier to understand like this:
remove all trailing and leading spaces: LTRIM(RTRIM(CompletionDetails))
replace 3 with comma: REPLACE( ?, '3', ',')
replace it with ' ': REPLACE(? , '.', ' ') or REPLACE(? , '.', '')

Removing trailing spaces and whitespaces from SQL Server column

I have a column in my SQL Server database and it has white spaces from left and right site of the record. Basically it's a nvarchar(250) column.
I have tried removing white spaces completely like this:
UPDATE MyTable
SET whitespacecolumn = LTRIM(RTRIM(whitespacecolumn))
But this didn't work out at all, the whitespace is still there. What am I doing wrong here?
Check the below;
Find any special characters like char(10), char(13) etc in the field value.
Check the status of ANSI_PADDING ON. Refer this MSDN article.
I think replace is the way as you are looking to update
UPDATE MyTable SET whitespacecolumn = Replace(whitespacecolumn, ' ', '')
you can try doing select first and then prefer to update
SELECT *, Replace(whitespacecolumn, ' ', '') from MyTable
LTRIM, RTRIM will remove spaces in front and rear of column. In 2016 you can use TRIM function as below to trim special characters as well:
SELECT TRIM( '.,! ' FROM '# test .') AS Result;
Output:
# test

Trimming empty string or spaces in SQL

What happens when you try and trim an empty string or a bunch of spaces in SQL Server? Does it become null or ''?
trimming will only leave you with a blank space.
ltrim(rtrim(' '))
but
nullif(ltrim(rtrim(' ')),'') will give you null
Here's an easy way to test string manipulations, manually throw some strings together and find out:
;with cte as (SELECT ' 0 ' AS col
UNION SELECT ' ' AS col
)
SELECT LTRIM(RTRIM(col)) as col
FROM cte
Which returns a zero and a blank string. To get a NULL you'd have to use NULLIF()
I run this command while updating all row.
Revise your need.
For example:
UPDATE TableName SET ColumnName = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(ColumnName, CHAR(10), CHAR(32)), CHAR(13), CHAR(32)), CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))

Removing blank spaces from the rows

I have a column as name which is having a results like.
name
ABC
XYZ
ader
fer
I want to remove the blank space before ader and it should print in the output like
ader.
How to achieve that?
Depending on your database you can use trim(), ltrim()/rtrim(), or replace():
select replace(name, ' ', '')
select trim(name, ' ')
select ltrim(rtrim(name))
You can use the LTRIM and RTRIM functions to remove trailing and leading spaces.
SELECT
RTRIM(LTRIM(name)) AS name
FROM yourTable