SQLite - remove all spaces - sql

Is there any way to remove all spaces using SQLite, kind of global trim(), not using any scripting language? I'm trying to hash values from multiple columns to track any changes but that will require to remove spaces for columns with multiple strings.
So far I wasn't able to find any tip related to that topic.

SQLite has some built-in string functions, including trim().
select trim(mycolumn) from mytable;
If you're trying to delete all spaces, you can use replace().
select replace(mycolumn, ' ', '') from mytable;
If you are trying to combine several columns you can use this with the concatenate operator.
select trim(col1) || trim(col2) || trim(col3) from mytable;

if you mean remove extra spaces
trim(replace(replace(replace(mycolumn, ' ', '| '), ' |',''), '|',''))

Related

Using RTRIM or REGEXP_REPLACE to replace a comma with a comma space and single quote

I'm attempting to learn Oracle regexp_replace well enough to take a value stored in a table as a comma-separated string and change the comma character with a single quote followed by a comma followed by a space, followed by a single quote.
For instance, the field (CourseListT) contains course codes that look like this:
PEOE100,H003,H102,L001,L100,L110,M005,M020,M130
I want it to look like this:
'PEOE100', 'H003', 'H102', 'L001', 'L100', 'L110', 'M005', 'M020', 'M130'
I started with baby steps and found article #25997057 here that showed me how to insert spaces. So I have this working:
SELECT
regexp_replace(gr.CourseListT,'([a-zA-Z0-9_]+)(,?)',' \1\2')
FROM gradreq gr
WHERE gr.gradreqsetid = 326
AND gr.SubjectArea = 'Electives'
But nothing I do will allow me to insert those silly single quote marks.
Would it be better to learn RTRIM replace? Could somebody please help me learn how to accomplish this?
Thank you
Schelly
You can simply do it with replace. Use double single-quotes to escape a single-quote.
select '''' || replace(CourseListT, ',', ''', ''') || ''''
from gradreq

SQL Replace multiple different characters in string

I need to replace multiple characters in a string. The result can't contain any '&' or any commas.
I currently have:
REPLACE(T2.[ShipToCode],'&','and')
Which converts & to and, but how do you put multiple values in one line?
You just need to daisy-chain them:
REPLACE(REPLACE(T2.[ShipToCode], '&', 'and'), ',', '')
One comment mentions "dozens of replace calls"... if removing dozens of single characters, you could also use Translate and a single Replace.
REPLACE(TRANSLATE(T2.[ShipToCode], '[];'',$#', '#######'), '#', '')
We used a function to do something similar that looped through the string, though this was mostly to remove characters that were not in the "#ValidCharacters" string. That was useful for removing anything that we didn't want - usually non-alphanumeric characters, though I think we also had space, quote, single quote and a handful of others in that string. It was really used to remove the non-printing characters that tended to sneak in at times so may not be perfect for your case, but may give you some ideas.
CREATE FUNCTION [dbo].[ufn_RemoveInvalidCharacters]
(#str VARCHAR(8000), #ValidCharacters VARCHAR(8000))
RETURNS VARCHAR(8000)
BEGIN
WHILE PATINDEX('%[^' + #ValidCharacters + ']%',#str) > 0
SET #str=REPLACE(#str, SUBSTRING(#str ,PATINDEX('%[^' + #ValidCharacters +
']%',#str), 1) ,'')
RETURN #str
END
If you need fine control, it helps to indent-format the REPLACE() nesting for readability.
SELECT Title,
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(RTRIM(Title),
' & ',''),
'++', ''),
'/', '-'),
'(',''),
')',''),
'.',''),
',',''),
' ', '-')
AS Title_SEO
FROM TitleTable
If you use SQL Server 2017 or 2019 you can use the TRANSLATE function.
TRANSLATE(ShipToCode, '|+,-', '____')
In this example de pipe, plus, comma en minus are all replaced by an underscore.
You can change every character with its own one.
So in the next example the plus and minus are replaced by a hash.
TRANSLATE(ShipToCode, '|+,-', '_#_#')
Just make sure the number of characters is the same in both groups.
Hope this might helps to anyone
If you want to replace multiple words or characters from a string with a blank string (i.e. wanted to remove characters), use regexp_replace() instead of multiple replace() clauses.
SELECT REGEXP_REPLACE("Hello world!123SQL$##$", "[^\w+ ]", "")
The above query will return Hello world123SQL
The same process will be applied if you want to remove multiple words from the string.
If you want to remove Hello and World from the string Hello World SQL, then you can use this query.
SELECT REGEXP_REPLACE("Hello World SQL", "(Hello|World)", "")
This will return SQL
With this process, the query will not look redundant and you didn't have to take care of multiple replace() clauses.
Conclusion
If you wanted to replace the words with blank string, go with REGEXP_REPLACE().
If you want to replace the words with other words, for example replacing & with and then use replace(). If there are multiple words to be replaced, use multiple nested replace().

Unwanted Spaces when concatenating fields and text strings

I'm putting the following in my SQL select statement to concatenate text strings (which are not fields in the database) with a couple database fields and I'm getting spaces where I try to use the to_char function to add leading zeros to a couple fields.
Running:
SELECT 'EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00') as ATS_NBR
Yields:
EP. 0017092. 01
How to I eliminate the unnecessary spaces?
Try using replace function:
SELECT replace(('EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00')), ' ', '') as ATS_NBR

How to concat_ws multiple fields and remove duplicate separators for empty slots

When you CONCAT_WS(' ',field1,field2,field3) in MySQL and if one of the fields is empty, not null, you get multiple separators.
An example can be:
John[space][space][space]Doe[space]III.
How can I make sure there is only one separator.
Do it like this:
CONCAT_WS(' ', NULLIF(field1, ''), NULLIF(field2, ''), NULLIF(field3, ''));
CONCAT_WS will skip any null values, and by using NULLIF any empty ones too.
Note: You can't regex replace. MySQL does not support it.

How to replace common words in sql column

I have a table of common words that are used in sentences (i.e. A, the, and, where, etc...)
What I want to do is loop through all those words and strip them out of the descriptions that people have entered to attempt to generate common keywords or tags. But I can't use replace because replace will remove any instance of the common word regardless of whether it is only a couple of letters that make up a larger word. For instance:
I want to replace A in the description. Now obviously a lot of words contain the letter a. So all those A's will be stripped from the words. I don't want that. I only want it when A is used a a whole word. I can figure this out using regular expressions but was wondering if there was anyway to do this in SQL without having to resort to CLR proc.
Maybe I am missing something but I couldn't seem to find an easy way to do this without having to write some specific scenarios like: word plus space before, word plus space after, word plus period after, etc... I don't think that is the best way.
For quick and dirty, I used to slosh through the various SQL functions PATINDEX, LEFT, RIGHT and LIKE to do this sort of thing. For one-time data prep, I export to something like Excel and eyeball it.
A good approach also is to create a new StringSubstitutionTable with two columns SOURCESTRING and TARGETSTRING and run a replace function to replace the SOURCESTRING with the TARGETSTRING on the joined table. This is cool because you can just add substitution entries as needed.
You can try nesting the replaces for each word you would like to replace. For example:
UPDATE TableName
SET ColumnName = REPLACE(REPLACE(REPLACE(REPLACE(TableName.ColumnName,' a ',' '),' the ',' '),' and ',' '), ' ', ' ')
Let me know if this is what you were looking for.
Here is they way I did something similar to what you are trying to do.
During your replace action...
Append a space before and after the common word.
Append a space before and after the description.
Let's suppose you want to remove the CommonWord "A" from the Description.
Description: "A good phrase never starts with A or ends with A"
CommonWord: "A"
Update TableName
Set Description =
LTRIM(RTRIM(Replace(' ' + Description + ' ', ' ' + CommonWord + ' ', ' ')))
This lets you delete all whole words that equal 'A'. Because you are replacing ' A ' with a space you need to LTRIM RTRIM to remove any leading or trailing spaces.
You could also do this in two steps:
--
-- Step 1 Loop through all common words removing them
--
Update TableName
Set Description = Replace(' ' + Description + ' ', ' ' + CommonWord + ' ', ' ')
--
-- Step 2 Unconditionally Trim all Descriptions
--
Update TableName
Set Description = LTRIM(RTIM(Description))