How to replace particular string in table column value in PostgreSQL The 2019 Stack Overflow Developer Survey Results Are InPostgreSQL “DESCRIBE TABLE”Show tables in PostgreSQLExport PostgreSQL table to CSV file with headingsModify OWNER on all tables simultaneously in PostgreSQLHow to import CSV file data into a PostgreSQL table?How can I drop all the tables in a PostgreSQL database?How to drop a PostgreSQL database if there are active connections to it?How to start PostgreSQL server on Mac OS X?How to exit from PostgreSQL command line utility: psqlHow to change PostgreSQL user password?
Why hard-Brexiteers don't insist on a hard border to prevent illegal immigration after Brexit?
How come people say “Would of”?
How technical should a Scrum Master be to effectively remove impediments?
What is the meaning of the verb "bear" in this context?
Aging parents with no investments
Multiply Two Integer Polynomials
What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?
Loose spokes after only a few rides
What is the meaning of Triage in Cybersec world?
Do these rules for Critical Successes and Critical Failures seem Fair?
Pokemon Turn Based battle (Python)
Is flight data recorder erased after every flight?
How to save as into a customized destination on macOS?
Time travel alters history but people keep saying nothing's changed
A poker game description that does not feel gimmicky
How to manage monthly salary
Apparent duplicates between Haynes service instructions and MOT
How to notate time signature switching consistently every measure
Can you compress metal and what would be the consequences?
Why do UK politicians seemingly ignore opinion polls on Brexit?
What is the accessibility of a package's `Private` context variables?
Output the Arecibo Message
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
If a Druid sees an animal’s corpse, can they wild shape into that animal?
How to replace particular string in table column value in PostgreSQL
The 2019 Stack Overflow Developer Survey Results Are InPostgreSQL “DESCRIBE TABLE”Show tables in PostgreSQLExport PostgreSQL table to CSV file with headingsModify OWNER on all tables simultaneously in PostgreSQLHow to import CSV file data into a PostgreSQL table?How can I drop all the tables in a PostgreSQL database?How to drop a PostgreSQL database if there are active connections to it?How to start PostgreSQL server on Mac OS X?How to exit from PostgreSQL command line utility: psqlHow to change PostgreSQL user password?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to replace certain text with other text in PostgreSQL.
To be more specific, I am trying to replace image path and anchor href in article
(table blog_posts
) from relative to absolute path. Some of the images and anchors already have an absolute path that should not get disturbed.
I tried to select the records which I need to modify:
SELECT
bp.id,
bp.article,
FROM
blog_posts bp
WHERE
bp.article LIKE '%src=%"/fixed_word/%'
OR
bp.article LIKE '%src="/fixed_word/%'
OR
bp.article LIKE '%href="/fixed_word/%'
OR
bp.article LIKE '%href=%"/fixed_word/%'
now I am not sure how to proceed further to update. Please help to get right solution.
My data is something like this:
MyTable: blog_posts
id article
1 any text <img any-atribute src="/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="/fixed_word/variable_word1/something.png"/> any text
5 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
OutPut should be:
id article
1 any text <img any-atribute src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"><img src="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_wordvariable_word1/something.png"/> any text
5 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png">
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
postgresql pattern-matching
add a comment |
I am trying to replace certain text with other text in PostgreSQL.
To be more specific, I am trying to replace image path and anchor href in article
(table blog_posts
) from relative to absolute path. Some of the images and anchors already have an absolute path that should not get disturbed.
I tried to select the records which I need to modify:
SELECT
bp.id,
bp.article,
FROM
blog_posts bp
WHERE
bp.article LIKE '%src=%"/fixed_word/%'
OR
bp.article LIKE '%src="/fixed_word/%'
OR
bp.article LIKE '%href="/fixed_word/%'
OR
bp.article LIKE '%href=%"/fixed_word/%'
now I am not sure how to proceed further to update. Please help to get right solution.
My data is something like this:
MyTable: blog_posts
id article
1 any text <img any-atribute src="/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="/fixed_word/variable_word1/something.png"/> any text
5 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
OutPut should be:
id article
1 any text <img any-atribute src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"><img src="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_wordvariable_word1/something.png"/> any text
5 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png">
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
postgresql pattern-matching
add a comment |
I am trying to replace certain text with other text in PostgreSQL.
To be more specific, I am trying to replace image path and anchor href in article
(table blog_posts
) from relative to absolute path. Some of the images and anchors already have an absolute path that should not get disturbed.
I tried to select the records which I need to modify:
SELECT
bp.id,
bp.article,
FROM
blog_posts bp
WHERE
bp.article LIKE '%src=%"/fixed_word/%'
OR
bp.article LIKE '%src="/fixed_word/%'
OR
bp.article LIKE '%href="/fixed_word/%'
OR
bp.article LIKE '%href=%"/fixed_word/%'
now I am not sure how to proceed further to update. Please help to get right solution.
My data is something like this:
MyTable: blog_posts
id article
1 any text <img any-atribute src="/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="/fixed_word/variable_word1/something.png"/> any text
5 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
OutPut should be:
id article
1 any text <img any-atribute src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"><img src="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_wordvariable_word1/something.png"/> any text
5 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png">
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
postgresql pattern-matching
I am trying to replace certain text with other text in PostgreSQL.
To be more specific, I am trying to replace image path and anchor href in article
(table blog_posts
) from relative to absolute path. Some of the images and anchors already have an absolute path that should not get disturbed.
I tried to select the records which I need to modify:
SELECT
bp.id,
bp.article,
FROM
blog_posts bp
WHERE
bp.article LIKE '%src=%"/fixed_word/%'
OR
bp.article LIKE '%src="/fixed_word/%'
OR
bp.article LIKE '%href="/fixed_word/%'
OR
bp.article LIKE '%href=%"/fixed_word/%'
now I am not sure how to proceed further to update. Please help to get right solution.
My data is something like this:
MyTable: blog_posts
id article
1 any text <img any-atribute src="/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="/fixed_word/variable_word1/something.png"/> any text
5 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
OutPut should be:
id article
1 any text <img any-atribute src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"><img src="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_wordvariable_word1/something.png"/> any text
5 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png">
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
postgresql pattern-matching
postgresql pattern-matching
edited Mar 8 at 11:19
VBMali
asked Mar 8 at 9:39
VBMaliVBMali
89111333
89111333
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This can be a starting point:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\1="https://mydomain.subdomain.com/fixed_word/\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' insrc=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!
– VBMali
Mar 8 at 10:21
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
If you havestandard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really usestandard_conforming_strings = on
.
– Laurenz Albe
Mar 11 at 14:07
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
|
show 13 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060427%2fhow-to-replace-particular-string-in-table-column-value-in-postgresql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be a starting point:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\1="https://mydomain.subdomain.com/fixed_word/\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' insrc=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!
– VBMali
Mar 8 at 10:21
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
If you havestandard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really usestandard_conforming_strings = on
.
– Laurenz Albe
Mar 11 at 14:07
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
|
show 13 more comments
This can be a starting point:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\1="https://mydomain.subdomain.com/fixed_word/\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' insrc=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!
– VBMali
Mar 8 at 10:21
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
If you havestandard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really usestandard_conforming_strings = on
.
– Laurenz Albe
Mar 11 at 14:07
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
|
show 13 more comments
This can be a starting point:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\1="https://mydomain.subdomain.com/fixed_word/\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
This can be a starting point:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\1="https://mydomain.subdomain.com/fixed_word/\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
edited Mar 12 at 6:46
answered Mar 8 at 10:09
Laurenz AlbeLaurenz Albe
52.8k103052
52.8k103052
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' insrc=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!
– VBMali
Mar 8 at 10:21
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
If you havestandard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really usestandard_conforming_strings = on
.
– Laurenz Albe
Mar 11 at 14:07
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
|
show 13 more comments
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' insrc=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!
– VBMali
Mar 8 at 10:21
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
If you havestandard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really usestandard_conforming_strings = on
.
– Laurenz Albe
Mar 11 at 14:07
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' in
src=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!– VBMali
Mar 8 at 10:21
Hi Laurenz, I again checked the possible bad data for the ` ` used in src or href path. fortunately there is not such data. so there is no worry about '' there will be always ' / ' in src or href. but there might be that starting '/' in
src=/"fixed_word...
Accordingly I need the help, I will be changing the question details for the same. Sorry for inconvenience!– VBMali
Mar 8 at 10:21
1
1
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
My answer should work then.
– Laurenz Albe
Mar 8 at 10:26
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
Error: parentheses () not balanced
– VBMali
Mar 8 at 11:11
1
1
If you have
standard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really use standard_conforming_strings = on
.– Laurenz Albe
Mar 11 at 14:07
If you have
standard_conforming_strings = off
, you need to double all backslashes. I'll edit the answer accordingly. But you should really use standard_conforming_strings = on
.– Laurenz Albe
Mar 11 at 14:07
1
1
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
I have removed all the backslashes since you don't need them. This simplifies the code. You should test it thoroughly before using it, since I won't guarantee correctness.
– Laurenz Albe
Mar 12 at 6:48
|
show 13 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060427%2fhow-to-replace-particular-string-in-table-column-value-in-postgresql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown