Replacing a space-looking character in SQL Server - sql

I have a database backup that I restored in SQL Server 2014 and I am using SQL Server Management Studio 18 (15.0.18206.0)
In a column of type text strings of XML files are stored. Yes, I know that the datatype text is obsolete. But I cannot change the datatype in the old program so easily.
Now I want to remove the string <SCHNITTLINIE>.
In the cell, it looks like this: ..._MIN> <SCHNITTLINIE> <S...
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), '<SCHNITTLINIE>', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'.
As a result, I get: ...MIN> <S...
So far so good.
I'd like to remove the "spaces" as well.
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), ' <SCHNITTLINIE> ', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'
Now he finds the string ' ' but not anymore.
At the beginning of the cell is a <?xml version="1.0". So I tried the following to test the spaces:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml ', 'xxx') AS TEXT)
FROM .[dbo].[XMLdatei].
WHERE xml LIKE '%SCHNITT%'.
As a result I got the following: <?xxxversion="1.0"
The following also works:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml' + char(32), 'xxx') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITT%'
Check, the test was successful. :)
Only with the string ' <SCHNITTLINIE> ' this does not work.
How can I find out what the characters before < or after > are?
How can I remove them without knowing what the characters are?
Thanks for your help.
Have a nice weekend.
Christoph

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.

How to remove space from phone number (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

How to split Arabic Words based on Connected ligature in SQL Server

How can I split Arabic words based on connected Ligature in SQL Server, e.g
أخبارى
أ - خبا - ر - ى
أخذتهم
أ - خذ - تهم
I have tried many solution but either they are based on spaces or any deliminator, in my case there is no space.
This is very rudimentary and should only be used as a starting point.
This is searching for each ligature and replacing that with an addition of a space.
DECLARE #word NVARCHAR(100) = N'أخبارى'
SELECT LEN(#word), #word
SELECT REPLACE(REPLACE(REPLACE(REPLACE(#word, N'أ', N'أ '), N'ى', N'ى '), N'ر', N'ر ' ), N'خب', N'خب ')
SELECT LEN(REPLACE(REPLACE(REPLACE(REPLACE(#word, N'أ', N'أ '), N'ى', N'ى '), N'ر', N'ر ' ), N'خب', N'خب ') )
You can create a table with all possible ligatures and query that using dynamic SQL following the above pattern.. I will provide an example to show what I mean

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)

SQL Command to replace embedded spaces with another character

I have a relational database with several fixed length character fields. I need to permanently replace all the embedded spaces with another character like - so JOHN DOE would become JOHN-DOE and ITSY BISTSY SPIDER would become ITSY-BISTSY-SPIDER. I can search before hand to make sure there are no strings that would conflict. I just need to be able to print the requested files with no embedded spaces. I would do the replacement in the C code but I want to make sure that there is never a future case where there is a JANE DOE and JANE-DOE in the DB.
By the way I have already made sure that there are no strings with more than one consecutive embedded space or leading spaces only trailing spaces to fill the fixed length fields.
Edit: thanks for all the help!
It looks like when I cut & pasted my question from Word to StackOverflow the trailing spaces got lost so the meaning my question was lost a bit.
I need to replace only the embedded spaces not the trailing spaces!
Note: I am using middle dot to stand in for spaces that don't show well.
Using:
SELECT REPLACE(operator_name, ' ', '-') FROM operator_info ;
the string JOHN·DOE············ became JOHN-DOE------------.
I need JOHN-DOE············.
I am thinking I need to use aliasing and the TRIM command but not sure how.
With whatever REPLACE function is built into your particular database.
MySQL:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
Oracle:
http://psoug.org/reference/translate_replace.html
SQLServer:
http://msdn.microsoft.com/en-us/library/ms186862.aspx
Edits below based on your comment.
I've done this in SQLServer syntax so please modify the example as needed. The first example really breaks down what's going on and the second one bunches it all into a single ugly query :D
#output in this case contains your final value.
DECLARE #input VARCHAR (100) = ' some test ';
DECLARE #trimmed VARCHAR (100);
DECLARE #replaced VARCHAR (100);
DECLARE #output VARCHAR (100);
-- Get just the inner text without the preceding / trailing spaces.
SET #trimmed = LTRIM (RTRIM (#input));
-- Replace the spaces *inside* the trimmed text with a dash.
SET #replaced = REPLACE (#trimmed, ' ', '-');
-- Take the original text and replace the trimmed version (with the inner spaces) with the dash version.
SET #output = REPLACE (#input, #trimmed, #replaced);
-- Show each step of the process!
SELECT #input AS INPUT,
#trimmed AS TRIMMED,
#replaced AS REPLACED,
#output AS OUTPUT;
And as a SELECT statement.
DECLARE #inputTable TABLE (Value VARCHAR (100) NOT NULL);
INSERT INTO #inputTable (Value)
VALUES (' some test '),
(' another test ');
SELECT REPLACE (Value,
LTRIM (RTRIM (Value)),
REPLACE (LTRIM (RTRIM (Value)), ' ', '-'))
FROM #inputTable;
If you are using MSSQL:
SELECT REPLACE(field_name,' ','-');
Edit: After the requirement about skipping the trailing spaces.
You can try this one-liner:
SELECT REPLACE(RTRIM(#name), ' ', '-') + SUBSTRING(#name, LEN(RTRIM(#name)) + 1, LEN(#NAME))
However I would recommend that you put it into a user defined function instead.
assuming SQL Server:
update TABLE set column = replace (column, ' ','-')
SELECT REPLACE(field_name,' ','-');
Edit: After the requirement about skipping the trailing spaces. You can try this one-liner:
SELECT REPLACE(RTRIM(#name), ' ', '-') + SUBSTRING(#name, LEN(RTRIM(#name)) + 1;