How to remove space from phone number (SQL) - sql

I have phone numbers in the following format:
03 12345678 and 0412 3456789
I need to remove the space from the numbers so that I can join to another table where number format is 0312345679 and 04123456789. I do not want to update the table.
I have tried to run the following query for the home number format, but keep getting an error:
SELECT
REPLACE(p.Home_Phone_Num, ' ', '') AS Home_Num
FROM table
The error:
Syntax error: expected something between the 'SELECT' keyword and the 'REPLACE' keyword.
Thanks

This looks like a Teradata error message. This database does not have a replace() function - instead, you need oreplace():
select oreplace(p.Home_Phone_Num, ' ', '') as Home_Num from mytable

To remove single characters there's no need for oReplace, use oTranslate instead:
oTranslate (p.Home_Phone_Num, ' ', '') AS Home_Num
This might also replace additional characters
oTranslate (p.Home_Phone_Num, ' -/()', '') AS Home_Num

Related

SQL SELECT WHERE without underscore and more

I want to select where 2 strings but without taking
underscore
apostrophe
dash..
Hello !
I want to select an option in my SQL database who look like this :
Chef d'équipe aménagement-finitions
With an original tag who look like this
chef-déquipe-aménagement-finitions
Some results in database had a - too
SELECT *
FROM table
WHERE REPLACE(name, '-', ' ') = REPLACE('chef-déquipe-aménagement-finitions', '-', ' ')
didnt work because of missing '
And a double replace didn't work too.
I want the string be able to compare without taking
underscore
apostrophe
dash
and all things like that
is this possible ?
Thanks for your help
Have good day !
Depends on your rdbms, but here's how I would perform in MySQL 8. If using a different version or rdbms, then first determine how to escape the single quote and modify as needed.
with my_data as (
select 'Chef d''équipe aménagement-finitions' as name
)
select name,
lower(replace(replace(name, '\'', ''), ' ', '-')) as name2
from my_data;
name
name2
Chef d'équipe aménagement-finitions
chef-déquipe-aménagement-finitions
Sql-server and Postgres version:
lower(replace(replace(name, '''', ''), ' ', '-')) as name
After posting, this, I re-read and noticed you are also looking to replace other characters. You could either keep layering the replace function, or, look into other functions.

My query works in SQL Server 2017, but it does not work in SQL Server 2014

I am trying to replace values in a delimited list that I am stuck with, and I know it is against the normalization. However I have no choice. I have written a query that replaces a value in delimited list and avoids duplication if the value I am trying to replace it with already exists.
My query works perfectly fine in SQL Server 2017, but it does not work in SQL Server 2014. I get an error message. I need this query to work in SQL Server 2014. I would appreciate it a lot. My query is given below along with fiddle execution link. The error message is
Msg 156 Level 15 State 1 Line 4
Incorrect syntax near the keyword 'FROM'.
Msg 102 Level 15 State 1 Line 14
Incorrect syntax near 'inputs'.
Msg 102 Level 15 State 1 Line 23
Incorrect syntax near 'replacement'.
https://dbfiddle.uk/rdbms=sqlserver_2014&fiddle=9eb71e1e90b07c8c150004dc9a6d5107
UPDATE test
SET appValue = TRIM(',' FROM REPLACE(inputs.expression, inputs.pattern, replacement.value))
FROM test
CROSS APPLY
(SELECT
',' + appValue + ',' AS expression,
',' + '406' + ',' AS pattern,
',' + '506' + ',' AS replacement
) inputs
CROSS APPLY
(SELECT
CASE
WHEN inputs.expression LIKE '%' + inputs.replacement + '%'
THEN ','
ELSE inputs.replacement
END) replacement(value)
WHERE
inputs.expression LIKE '%' + inputs.pattern + '%'
The TRIM() function is new for Sql Server 2017. For older versions of Sql Server you have to use the older LTRIM() and RTRIM() functions, which don't have the same characters FROM string syntax, or other more complicated string functions like CHARINDEX() and SUBSTRING().
The easiest thing here might be to REPLACE() all spaces with a special character you know is not part of your data, then REPLACE() all commas with a space, trim the spaces using the old LTRIM()/RTRIM() functions, REPLACE() the spaces back to commas again, and REPLACE() the special character back to spaces.
REPLACE(REPLACE(RTRIM(LTRIM(REPLACE(REPLACE(column, ' ', '|'), ',', ' '))), ' ', ','), '|', ' ')
Or if you know there is likely to be just one extra trailing or leading comma, you can test for that and use SUBSTRING() to cut out just that character.

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

SQL- Remove Trailing space and add it to the beginning of the Name

Was working on SQL-EX.ru exercises.
There is one question for DML that I could not do, but I cannot proceed to the next one, until this one is done.
the question itself: All the trailing spaces in the name column of the Battles table remove and add them at the beginning of the name.
My code:
Update Battles
set name=concat(' ',(LTRIM(RTRIM(name))))
The system does not let it go through, I understand that I am using ' ' for the concat, whereas I need to use the stuff that got trimmed. And I have no idea how...
Any help would be very much appreciated
Try Something Like:-
set name = lpad(trim(name), length(trim(name))+4, ' ')
Here use TRIM to remove space from both side. use LPAD to add something on left side with n (4) chars
I'm not familiar with SQL-EX.ru, but if it's Oracle compatible and you can use regular expressions (or you are at that point in the training) here's a way. Maybe it'll give you an idea at least. The first part is just setup and uses a WITH clause to create a table (like a temp table in memory, actually called a Common Table Expression or CTE) called battles containing a name column with 2 rows. Each name column datum has a different number of spaces at the end. Next select from that column using a regular expression that uses 2 "remembered" groups surrounded by parentheses, the first containing the string up to until but not including the first space, the second containing 0 or more space characters anchored to the end of the line. Replace that with the 2nd group (the spaces) first, followed by the first group (the first part of the string). This is surrounded by square brackets just to prove in the output the same spaces were moved to the front of the string.
SQL> with battles(name) as (
select 'test2 ' from dual union
select 'test1 ' from dual
)
select '[' || regexp_replace(name, '(.*?)([ ]*)$', '\2\1') || ']' fixed
from battles;
FIXED
----------------------------------------------------------------------------
[ test1]
[ test2]
SQL>
I hope this solution can be applied to your problem or at least give you some ideas.
Try this:
set name = case when len(name) > len(rtrim(name))
then replicate(' ', len(name) - len(rtrim(name))) + rtrim(name)
else name
end
update battles
set name = case when (len(name+'a')-1) > len(rtrim(name))
then
replicate(' ',
(len(name+'a')-1) - len(rtrim(name))) + rtrim(name)
else name
end
Len() doesn't count trailing spaces. So using (len(name+'a')-1).
Simplest answer:
UPDATE Battles
SET name = SPACE(DATALENGTH(name)-DATALENGTH(RTRIM(name))) + RTRIM(name)
But only works because name is VARCHAR.
More generic is to do:
UPDATE Battles
SET name = SPACE(len(name+'x')-1-len(RTRIM(name))) + RTRIM(name)
simple example below ... enjoy :)
update battles set name =
Space( DATALENGTH(name) - DATALENGTH(rtrim(name))) + rtrim(name)
where date in ( select date from battles)

REPLACE not working on sql table column

I've tried select REPLACE(' this is a user name', ' ', '') and it gives me 'thisisausername' which is supposed to be.
My problem is, when I try to use REPLACE on selecting a table column, it doesn't work!
My query:
SELECT REPLACE(UserName, ' ', '') as UserName FROM MY_TABLE
it still gives me usernames with spaces! Am I doing something stupid?
#AlexK. it's 160 for unicode(left(field, 1))
160 is Unicode NO-BREAK SPACE so that's what you need to replace:
replace(UserName, char(160), '')
You could update everything replacing char(160) with a whitespace ' ' and then just use your original query in the future (perhaps also ensuring such values cannot be entered in the future)