I want to update a row which has some html tags inside. For instance:
src='/imagem.png'></ p></ body>
> UPDATE ISTANBUL_TABLE SET TEXT = '<
> body>< p>< img src='/imagem.png '></
> p></ body>' WHERE 1=1
You see after src=' means the query ends, but it does not end. How can i solve it without using " (double comma)? Any solution please?
best regards bk
You need to escape the single-quotes, by typing them twice:
UPDATE ISTANBUL_TABLE SET TEXT = '< body>< p>< img src=''/imagem.png ''>' WHERE 1=1
Also, your WHERE clause is nonsensical and can be dropped entirely
UPDATE ISTANBUL_TABLE SET TEXT = '<body><p><img src=''/imagem.png''>'
Use parameterised SQL:
UPDATE ISTANBUL_TABLE SET TEXT = #HTML WHERE...
Then from your calling code, you just pass in the #HTML parameter and don't need to double up the single quotes.
Related
I have column A with value hello.
I need to migrate it into new column AJson with value ["hello"].
I have to do this with Sql Server command.
There are different commands FOR JSON etc. but they serialize value with column name.
This is the same value that C# method JsonConvert.SerializeObject(new List<string>(){"hello"} serialization result would be.
I can't simply attach [" in the beginning and end because the string value may contain characters which without proper serialization will break the json string.
My advice is you just make a lot of nested replaces and then do it yourself.
FOR JSON is intended for entire JSON, and therefore not valid without keys.
Here is a simple example that replaces the endline with \n
print replace('ab
c','
','\n')
Backspace to be replaced with \b.
Form feed to be replaced with \f.
Newline to be replaced with \n.
Carriage return to be replaced with \r.
Tab to be replaced with \t.
Double quote to be replaced with "
Backslash to be replaced with \
My approach was to use these 3 commands:
UPDATE Offers
SET [DetailsJson] =
(SELECT TOP 1 [Details] AS A
FROM Offers AS B
WHERE B.Id = Offers.Id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
UPDATE Offers
SET [DetailsJson] = Substring([DetailsJson], 6, LEN([DetailsJson]) - 6)
UPDATE Offers
SET [DetailsJson] = '[' + [DetailsJson] + ']'
..for op's answer/table..
UPDATE Offers
SET [DetailsJson] = concat(N'["', string_escape([Details], 'json'), N'"]');
declare #col nvarchar(100) = N'
a b c " : [ ] ]
x
y
z'
select concat(N'["', string_escape(#col, 'json'), N'"]'), isjson(concat(N'["', string_escape(#col, 'json'), N'"]'));
I want delete h1 tags and text between them from post_content sql column.
I have tried something like this:
DELETE FROM wpbz_posts
WHERE `post_content` like '<h1>%</h1>';
but don't have a clue, how to work with text between tags, because every text line is different than others.
Any hints or help? Thanks!
You could UPDATE with a REGEXP_REPLACE to strip the "h1" tags and what's in them
UPDATE wpbz_posts
SET post_content = REGEXP_REPLACE(post_content,'<h1>.*?</h1>','')
WHERE post_content LIKE '%</h1>%'
The ? in .*? makes it a lazy search, so it only matches till the first </h1> that follows the <h1> .
Test on db<>fiddle here
There's also an UPDATEXML function, but that only replaces 1 occurence of a tag.
And will raise an "Incorrect XML value" error if the string doesn't contain a valid XML.
UPDATE wpbz_posts
SET post_content = UpdateXML(post_content,'//h1[1]','')
WHERE post_content LIKE '%</h1>%'
in my table within some columns i got strings which starting always with /PicsDB
like this below:
/PicsDB/Something 2015/Some thing bla/Some thing other/img34234.jpg
what i want to achieve is to for each row delete starting string /PicsDB
so using above string the final result should be:
/Something 2015/Some thing bla/Some thing other/img34234.jpg
How to achieve that?
Can i just simply do ? :
UPDATE my_table SET path = replace(path, '/PicsDB', '');
Just use substring:
UPDATE my_table SET path = substring(path, 8, 9999);
where path like '/PicsDB%'
I am spooling a package from a database and this is what I get:
CREATE OR REPLACE PACKAGE BODY "CPI"."GIPI_WBOND_BASIC_PKG"
AS
FUNCTION get_gipi_wbond_basic (p_par_id gipi_wbond_basic.par_id%TYPE)
RETURN gipi_wbond_basic_tab PIPELINED
IS
v_wbond gipi_wbond_basic_type;
BEGIN
FOR i IN (SELECT a.par_id, a.obligee_no, a.bond_dtl, a.inde
mnity_text,
a.clause_type, a.waiver_limit, a.contract_date, a.cont
ract_dtl,
a.prin_id, a.co_prin_sw, a.np_no, a.coll
_flag,
a.plaintiff_dtl, a.defendant_dtl, a.civil_case_no
FROM gipi_wbond_basic a
WHERE a.par_id = p_par_id)
And I am expecting it to be something like this:
CREATE OR REPLACE PACKAGE BODY cpi.gipi_wbond_basic_pkg
AS
FUNCTION get_gipi_wbond_basic (p_par_id gipi_wbond_basic.par_id%TYPE)
RETURN gipi_wbond_basic_tab PIPELINED
IS
v_wbond gipi_wbond_basic_type;
BEGIN
FOR i IN (SELECT a.par_id, a.obligee_no, a.bond_dtl, a.indemnity_text,
a.clause_type, a.waiver_limit, a.contract_date,
a.contract_dtl, a.prin_id, a.co_prin_sw, a.np_no,
a.coll_flag, a.plaintiff_dtl, a.defendant_dtl,
a.civil_case_no
FROM gipi_wbond_basic a
WHERE a.par_id = p_par_id)
Please help me on how can I get rid of those new lines and ugly format. Thanks!
Ok this one solved my problem.
From this,
SET HEADING OFF;
SET ECHO OFF;
SET PAGES 999;
SET LONG 999999;
I added this:
SET LONGCHUNKSIZE 999999;
SET PAGESIZE 0;
SET LINESIZE 500;
TO remove extra Line breaks Try :-
SET FEED OFF
Additionally (to ladiesman1792's own answers, which were below this post when I posted!):
column text format a120 --<< will wrap at 120 chars (maybe bytes, not sure on that)
This is an Oracle sqlplus directive, as per the other answers.
You can do it using the regular expression in SSMS:
1) Ctrl-H to bring up the Find And Replace window
2) Select USE -> Regular Expressions
3) Put ^\n in the Find What
4) Keep Replace With empty
5) Click Replace (All)
Good luck
-- Nilesh Umaretiya (India)
If I want to update a column by pulling out only a part of a substring of another column.
What I want to do is pull the name of the jpg from the file and for example i want imageName to be equal to great-family.jpg" a varchar string. But the image names are all different.
update tblPetTips
set imageName = "great-family.jpg"
where articleText = "<img src="/images/imgs/great-family.jpg" alt="A Great Family Dog">"
In this case I would like to say
update tblPetTips
set imageName = "yellow-smile.jpg"
where articleText = "<img src="/images/imgs/yellow-smile.jpg" alt="A Yellow Smiley Face">"
How do I (without hardcoding) update imageName fromthe articleText column.
All the directories are the same - all the images live in images/imgs.
if the source of your images is always /images/imgs/, you can use patindex to find the position of '/images/imgs/' and '" alt', then extract the text between them.
check if this works:
substring(articletext,
patindex('/images/imgs/', articletext) + length('/images/imgs/'),
patindex('" alt') - (patindex('/images/imgs/', articletext) + length('/images/imgs/')))
if your images can have any url, then it would be feasible with regular expressions, but I don't think sqlserver provides regex directly.
in that case you could write a function to extract the filename part of a url and call it in the update.
You can try to get values between the last / and " with SUBSTRING_INDEX function:
UPDATE tblPetTips
SET imageName = SUBSTRING_INDEX(SUBSTRING_INDEX(articleText, '/', -1), '"', 1);
It will only work if format of <img srs=... > html is consistent.