querydsl - remove whitespace in the middle of a COLUMN - replace

Is there a way to do this query in querydsl?
SELECT *
FROM table
WHERE replace(column_name, ' ', '') = 'someValue';
The StringPath from the QClass has no .replace() function and it's necessary for some characters (specifically, spaces in the middle) to be removed from column_name before testing it with someValue.
Sample column_name contents: ABC, DEF, AB *
If someValue is ABC, ABC and AB* should appear.

You can express the replace invocation via
Expressions.stringTemplate("replace({0},' ','')", columnPath)

Related

SQLite Pattern Matching with Extra Character

My database contains these rows:
DuPage
Saint John
What queries could I use that would match people entering either 'Du Page' or 'SaintJohn': in other words: adding an extra character (at any position) that shouldn't be there, or removing a character (at any position) that should be there?
The first example has a possible workaround: I could just remove the space character from the 'Du Page' input before searching the table, but I cannot do that with the second example unless there was some way of saying 'match 'SaintJohn' with the database text that has had all spaces removed', or alternatively 'match a database row that has every letter in 'SaintJohn' somewhere in the row.
Remove spaces from the column and the search text:
select * from tablename
where replace(textcolumn, ' ', '') like '%' || replace('<your search string>', ' ', '') || '%'

Postgresql replace all occurrences of string+

I have this string:
this is the abcd xxx
string I want to abcd yyy
replace in my text abcd zzz
Now I want to replace abcd and anything after it with blank.
I want this result:
this is the
string I want to
replace in my text
I tried:
select regexp_replace(str, 'abcd.*','','gi')
But it just removed everything after the first match. Also other combos without luck.
What am I missing?
Thanks!
Use the flag n (newline-sensitive matching) in regexp_replace():
with my_table(str) as (
values(
'this is the abcd xxx
string I want to abcd yyy
replace in my text abcd zzz')
)
select regexp_replace(str, 'abcd.*','','gin')
from my_table
regexp_replace
-----------------
this is the +
string I want to +
replace in my text
(1 row)

Teradata regexp_replace to eliminate specific special characters

I imported a file that contains email addresses (email_source). I need to join this table to another, using this field but it contains commas (,) and double quotes (") before and after the email address (eg. "johnsmith#gmail.com,","). I want to replace all commas and double quotes with a space.
What is the correct syntax in teradata?
Just do this:
REGEXP_REPLACE(email_source, '[,"]', ' ',1,0,i)
Breakdown:
REGEXP_REPLACE(email_source, -- sourcestring
'[,"]', -- regexp
' ', --replacestring
1, --startposition
0, -- occurrence, 0 = all
'i' -- match -> case insensitive
)
You don't need a regex for this, a simple oTranslate should be more efficient:
oTranslate(email_source, ',"', ' ')

PostgreSQL regexp_replace() to keep just one whitespace

I need to clean up a string column with both whitespaces and tabs included within, at the beginning or at the end of strings (it's a mess !). I want to keep just one whitespace between each word. Say we have the following string that includes every possible situation :
mystring = ' one two three four '
2 whitespaces before 'one'
1 whitespace between 'one' and 'two'
4 whitespaces between 'two' and 'three'
2 tabs after 'three'
1 tab after 'four'
Here is the way I do it :
I delete leading and trailing whitespaces
I delete leading and trailing tabs
I replace both 'whitespaces repeated at least two' and tabs by a sole whitespace
WITH
t1 AS (SELECT' one two three four '::TEXT AS mystring),
t2 AS (SELECT TRIM(both ' ' from mystring) AS mystring FROM t1),
t3 AS (SELECT TRIM(both '\t' from mystring) AS mystring FROM t2)
SELECT regexp_replace(mystring, '(( ){2,}|\t+)', ' ', 'g') FROM t3 ;
I eventually get the following string, which looks nice but I still have a trailing whitespace...
'one two three four '
Any idea on doing it in a more simple way and solving this last issue ?
Many thanks !
SELECT trim(regexp_replace(col_name, '\s+', ' ', 'g')) as col_name FROM table_name;
Or In case of update :
UPDATE table_name SET col_name = trim(regexp_replace(col_name, '\s+', ' ', 'g'));
The regexp_replace is flags are described on this section of the documentation.
SELECT trim(regexp_replace(mystring, '\s+', ' ', 'g')) as mystring FROM t1;
Posting an answer in case folks don't look at comments.
Use '\s+'
Not '\\s+'
Worked for me.
It didn't work for me with trim and regexp_replace. So I came with another solution:
SELECT trim(
array_to_string(
regexp_split_to_array(' test with many spaces for this test ', E'\\s+')
, ' ')
) as mystring;
First regexp_split_to_array eliminates all spaces leaving "blanks" at the beginning and the end.
-- regexp_split_to_array output:
-- {"",test,with,many,spaces,for,this,test,""}
When using array_to_string all the ',' become spaces
-- regexp_split_to_array output ( '_' instead of spaces for viewing ):
-- _test_with_many_spaces_for_this_test_
The trim is to remove the head and tail
-- trim output ( '_' instead of spaces for viewing ):
-- test_with_many_spaces_for_this_test

How to convert those characters not quoted to upper case?

To format thousands of SQL queries I need to convert all the characters not in quotation mark pair to upper case.
For example:
select * from region where regionkey = 'America'
to be converted to
SELECT * FROM REGION WHERE REGIONKEY = 'America'
With perl I'm able to convert those quoted characters to upper case by:
perl -p -e 's/('.+?')/\U\1/g'
and get:
select * from region where regionkey = 'AMERICA'
The question is how to "reverse" the capture result, say, to march to not in quotation marks?
s/([^']*)('[^']*'|\z)/\U$1\E$2/g
so
perl -pe's/([^'\'']*)('\''[^'\'']*'\''|\z)/\U$1\E$2/g'
ysth suggests a mixed-quote approach:
perl -pe"s/([^']*)('[^']*'|\z)/"'\U$1\E$2/g'
If the quotes can have backslash escapes in them, change
'[^']*'
to
'(?:[^'\\]+|\\.)*'
Split the string on quoted substrings and uppercase every other chunk. Like this
my $str = "select * from region where regionkey = 'America'";
my $uc;
$str = join '', map { ($uc = !$uc) ? uc : $_ } split /('[^']*')/, $str;
print $str;
output
SELECT * FROM REGION WHERE REGIONKEY = 'America'