I have DB in Postgresql 12 and PgAdmin 4 and a lot of queries like:
update public.register set address = replace(address,'Á','A') where address like '%Á%';
update public.register set address = replace(address,'Â','A') where address like '%Â%';
update public.register set address = replace(address,'Ã','A') where address like '%Ã%';
update public.register set address = replace(address,'Ä','A') where address like '%Ä%';
update public.register set address = replace(address,'Å','A') where address like '%Å%';
I want to declare arrays like this:
SET array1 = ARRAY['Á','Â','Ã','Ä','Å]
SET array2 = ARRAY['A','A','A','A','A']
and make a loop just like:
for(i=0; i<count(array1[]); i++){
update public.register set address = replace(address,array1[i],array2[i]) where address like '%array1[i]%';
}
I have not found any good and working idea on forums.
Thanks for any info about this :)
You don't need a loop. This can be done using a single UPDATE statement using regexp_replace()
update register
set address = regexp_replace(address, '[ÁÂÃÄÅ]', 'A', 'g')
where address ~ '[ÁÂÃÄÅ]';
'[ÁÂÃÄÅ]' matches each character in the list. The 'g' option will replace all occurrences in the value.
The WHERE clause then makes sure that only those rows that contain these characters in the address column are changed.
It would work without the WHERE as well, as the regexp_replace() will not change the address if the characters aren't contained. But it's good coding practice to only update those rows that do actually change.
Another option is to install the unaccent extension, then you can use:
update register
set address = unaccent(address)
where address <> unaccent(address);
This has the additional benefit, that you can use language specific rules if you want.
Related
Tired brain - perhaps you can help.
My table has two bit fields:
1) TestedByPCL and
2) TestedBySPC.
Both may = 1.
The user interface has two corresponding check boxes. In the code I convert the checks to int.
int TestedBySPC = SearchSPC ? 1 : 0;
int TestedByPCL = SearchPCL ? 1 : 0;
My WHERE clause looks something like this:
WHERE TestedByPCL = {TestedByPCL.ToString()} AND TestedBySPC = {TestedBySPC.ToString()}
The problem is when only one checkbox is selected I want to return rows having the corresponding field set to 1 or both fields set to 1.
Now when both fields are set to 1 my WHERE clause requires both check boxes to be checked instead of only one.
So, if one checkbox is ticked return records with with that field = 1 , regardless of whether the other field = 1.
Second attempt (I think I've got it now):
WHERE ((TestedByPCL = {chkTestedByPCL.IsChecked} AND TestedBySPC = {chkTestedBySPC.IsChecked})
OR
(TestedByPCL = 1 AND TestedBySPC = 1 AND 1 IN ({chkTestedByPCL.IsChecked}, {chkTestedBySPC.IsChecked})))
Misunderstood the question.
Change the AND to an OR:
WHERE TestedByPCL = {chkTestedByPCL.IsChecked} OR TestedBySPC = {chkTestedBySPC.IsChecked}
Also:
SQL Server does not have a Boolean data type, it's closest option is a bit data type.
The usage of curly brackets suggests using string concatenations to build your where clause. This might not be a big deal when you're handling checkboxes but it's a security risk when handling free text input as it's an open door for SQL injection attacks. Better use parameters whenever you can.
I have a vertex that contains a string. I would like to update this string by adding a string to the end. The outcome should be the original string + the new string. I thought I could do this with append but it keeps making an embedded set. The code below returns ['123abc'] instead of '123abc'
I've posted a similar question here, but did not find a remedy. This seems like a simple sql command but I do not know the code. Please note I have also tried using unwind but it did not work. Thank you.
CREATE VERTEX V SET foo = '123'
UPDATE V SET foo = (SELECT foo.append('abc')FROM(SELECT FROM V WHERE foo = '1'))
You can do it using
UPDATE V SET foo = foo.append('abc') where foo = '123'
I want to set the chosen field to true in table e:\ctw for all records of field name which are not like name2 in the d:\le table. The 'ctw.name' records have equal or more characters than the le.name2 records so I want to implement wild card searches; for example,
le.name2 = abc,
ctw.name = abc\12a
So, I want to use a wild card search like 'abc%', but without hard coding a record value. That is, I want to know whether it is possible to combine a field name with wildcard search as below:
UPDATE ctw SET chosen = .t. WHERE LOWER(name) NOT LIKE ((select LOWER(name2) from le)+'%')
I get a function name is missing ) error upon command input.
Try it like this:
SET ANSI OFF
UPDATE ctw ;
SET chosen = .t.
WHERE LOWER(name) NOT IN (select LOWER(name2) from le)
Probably this is what you mean:
UPDATE ctw SET chosen = .t. ;
from le ;
where LOWER(name) LIKE lower(trim(le.Name2))+'%'
And because, by default VFP comparisons in SQL are not ANSI this would mean the same:
UPDATE ctw SET chosen = .t. ;
from le ;
where LOWER(name) = lower(trim(le.Name2))
But I wouldn't trust the non-ANSI implementation and use the ANSI implementation with LIKE.
I've having problem with regular expression for updating the multiple set for email that doesn't work at all. You see I'm trying to update from.
alisonsmith#gmail.com
bobgraves#hotmail.com
smithers#yahoo.com
011013092949#msn.ca
011513025559#aol.ca
101513025559#MSN.COM
To the result should look like this:
alisonsmith#dony.com
bobgraves#dony.com
smithers#dony.com
011013092949#dony.com
011513025559#dony.com
101513025559#dony.com
I've tried that update procedure like this and it didn't work at all:
update dony_membership
set Email = LEFT(Email,12)+'#dony.com'
set Email = LEFT(Email,0)+'#dony.com'
where Email LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]#%'
or Email LIKE'#%'
As it seems like you want to change the part after the #-sign for all rows I wouldn't bother with regular expressions but rather just keep what ever comes before the #-sign and append the new domain (dony.com) to it:
update dony_membership
set email = left(email, charindex('#', email, 0)) + 'dony.com'
Sample SQL Fiddle for your viewing pleasure :)
scenarioi:-
1) User enters an address into a search field. Address maybe zip,state or country
2) Now i have to take that address which may be in any order or which may just contain the country, state or zip and check the database if the the results exists or not.
Any advice on this topic would be awesome
How about this one?
'SELECT * FROM table WHERE address LIKE \''. addslashes($search) .'\'
OR zip = '. (int)$search .'
OR country LIKE \''. addslashes($search) .'\''
I would do something like :
split address on space
build where clause as
WHERE zip||state||country like '%address_part_one%'
OR zip||state||country like '%address_part_two%'
and so on ...
If you have fulltext enabled for the three fields :
WHERE MATCH (zip,state,country) AGAINST ('address_part_one')
OR MATCH (zip,state,country) AGAINST ('address_part_two')
...