In PostgreSQL a column contains values such as:
"Sample1"
"Sample2"
Is there an efficient way to go through each record removing the " " marks?
This seems like a relatively straightforward operation, but I am baffled.
Try this:
UPDATE TableName
SET ColName = REPLACE(ColName, '"', '');
WHERE ColName SIMILAR TO '%"%'
You can use LIKE operator as well instead of SIMILAR TO.
Since you asked for an efficient way:
UPDATE tbl
SET col = translate(col, '"', '')
WHERE col LIKE '%"%';
Never use SIMILAR TO, its an utterly pointless operator, only included in Postgres for standard compliance. There is hope the standard will include a regular expression match (like ~ in Postgres) instead in the future.
For replacing single characters, translate() is simpler and faster than replace().
If you only want to replace leading and / or trailing characters, use trim() / ltrim() / rtrim() instead. And also change to WHERE col LIKE '"%' OR col LIKE '%"' respectively.
Related
I have a lot of character varying records in this format: {'address': 'New Mexico'}.
I would like to update all those columns to have it like this: New Mexico.
I've been investigating how to do it, and it could be with regexp, but I don't know how to make for all columns in a table, and I never used regex in PostgreSQL before.
I have an idea that is something like this:
SET location = regexp_replace(field, 'match pattern', 'replace string', 'g')
Valid JSON literals require double-quotes where your sample displays single quotes. Maybe you can fix that upstream?
To repair (assuming there are no other, unrelated single-quotes involved):
UPDATE web_scraping.iws_informacion_web_scraping
SET iws_localizacion = replace(iws_localizacion, '''', '"')::json ->> 'address'
WHERE iws_id = 3678
AND iws_localizacion IS DISTINCT FROM replace(iws_localizacion, '''', '"')::json ->> 'address';
The 2nd WHERE clause prevents updates to rows that wouldn't change. See:
How do I (or can I) SELECT DISTINCT on multiple columns?
Optional if such cases can be excluded.
I have to do a simple select query with the LIKE operator but the problem is that the string to be compared with the LIKE operator contains the symbol "_", for example:
TRY_1245
POSTM_A422
PREP_1000X
And in the table on the DB i have:
TRY_
POSTM_
PREP_
How can I make the query work like this? because if I do the simple LIKE "TRY_1235"
the query returns nothing.
Thanks a lot in advance everyone!
I think you want:
WHERE 'TRY_1235' LIKE col || '%'
The column can be the pattern.
Note that this will match 'TRYST' because _ is a wildcard character. Perhaps the simplest solution is REGEXP_LIKE() -- assuming there are no other unusual characters:
WHERE REGEXP_LIKE('TRY_12345', '^' || col)
Or just escape the _:
WHERE 'TRY_12345' LIKE REPLACE(col, '_', '\_') || '%'
I need a like expression that will match a character whether or not it exists. It needs to match the following values:
..."value": "123456"...
..."value": "123456"...
"...value":"123456"...
This like statement will almost work: LIKE '%value":%"123456"%'
But there are values like this one that would also match, but I don't want returned:
..."value":"99999", "other":"123456"...
A regex expression to do what I'm looking to do is 'value": *?"123456"'. I need to do this in SQL Server 2008 and I don't believe there is good regex support in that version. How can I match using a like statement?
Remove the whitespace in your compare with REPLACE():
WHERE REPLACE(column,' ','') LIKE '%"value":"123456"%'
May need a double replace for tabs:
REPLACE(REPLACE(column,' ',''),' ','')
I don't think you can with the like operator. You could exclude ones you could match, like if you want to make sure it just doesn't contain other:
[field] LIKE '%value":%"123456"%` AND [field] NOT LIKE '%"other"%'
Otherwise I think you'd have to do some processing on the string. You could write a UDF to take the string and parse it to find the value for 'value' and compare based on that:
dbo.fn_GetValue([field], 'value') = '123456'
The function could find the index of '"' + #name + '"', find the next index of a quote, and the one after that, then get the string between those two quotes and return it.
I have postgres function in which i am appending values in query such that i have,
DECLARE
clause text = '';
after appending i have some thing like,
clause = "and name='john' and age='24' and location ='New York';"
I append above in where clause of the query i already have. While executing query i am getting "and" just after "where" result in error
How to use regex_replace so that i remove the first "and" from clause before appending it to the query ?
Instead of fixing clause after the fact, you could avoid the problem by using
concat_ws (concatenate with separator):
clause = concat_ws(' and ', "name='john'", "age='24'", "location ='New York'")
will make clause equal to
"name='john' and age='24' and location ='New York'"
This can be even simpler. Use right() with a negative offset.
Truncates the first n characters and you don't need to specify the length of the string. Faster, simpler.
Double quotes (") are for identifiers in Postgres (and standard SQL) and incorrect in your example. Enclose string literals in single quotes (') and escape single quotes within - or use dollar quoting:
Insert text with single quotes in PostgreSQL
Since this is a plpgsql assignment, use the proper assignment operator :=. The SQL assignment operator = is tolerated, too, but can lead to ambiguity in corner cases.
Finally, you can assign a variable in plpgsql at declaration time. Assignments in plpgsql are still cheap but more expensive than in other programming languages.
DECLARE
clause text := right($$and name='john' and age='24' ... $$, -5)
All that said, it seems like you are trying to work with dynamic SQL and starting off on the wrong foot here. If those values can change, rather supply them as values with the USING clause of EXECUTE and be wary of SQL injection. Read some of the related questions and answers on the matter:
https://stackoverflow.com/search?q=[plpgsql]+[dynamic-sql]+EXECUTE+USING
You do not need regex:
clause = substr(clause, 5, 10000);
clause = substr(clause, 5, length(clause)- 4); -- version for formalists
concat_ws sounds like the best option, but as a general solution for things like this (or any sort of list with a delimiter) you can use logic like (pseudocode):
delim = '';
while (more appendages)
clause = delim + nextAppendage;
delim = ' AND ';
If you want to do it with regular expression try this:
result = regexp_replace(clause, '^and ', '')
Im curious if and how you can use regular expressions to find white space in SQL statments.
I have a string that can have an unlimited amount of white space after the actual string.
For example:
"STRING "
"STRING "
would match, but
"STRING A"
"STRINGB"
would not.
Right now I have:
like 'STRING%'
which doesnt quite return the results I would like.
I am using Sql Server 2008.
A simple like can find any string with spaces at the end:
where col1 like '% '
To also allow tabs, carriage returns or line feeds:
where col1 like '%[ ' + char(9) + char(10) + char(13) + ']'
Per your comment, to find "string" followed by any number of whitespace:
where rtrim(col1) = 'string'
You could try
where len(col1) <> len(rtrim(col1))
Andomar's answer will find the strings for you, but my spidey sense tells me maybe the scope of the problem is bigger than simply finding the whitespace.
If, as I suspect, you are finding the whitespace so that you can then clean it up, a simple
UPDATE Table1
SET col1 = RTRIM(col1)
will remove any trailing whitespace from the column.
Or RTRIM(LTRIM(col1)) to remove both leading and trailing whitespace.
Or REPLACE(col1,' '.'') to remove all whitespace including spaces within the string.
Note that RTRIM and LTRIM only work on spaces, so to remove tabs/CRs/LFs you would have to use REPLACE. To remove those only from the leading/trailing portion of the string is feasible but not entirely simple. Bug your database vendor to implement the ANSI SQL 99 standard TRIM function that would make this much easier.
where len(col1 + 'x') <> len(rtrim(col1)) + 1
BOL provides workarounds for LEN() with trailing spaces : http://msdn.microsoft.com/en-us/library/ms190329.aspx
LEN(Column + '_') - 1
or using DATALENGTH