Search and replace serialised text in a column in mysql (without unserialising?) - search

I am trying to search and replace a specific text in a column in mssql based on specific search criteria. I am simply getting it wrong.
I have tried to use the 2 examples from this thread but cant get it right - Updating serialised array in mysql (without unserialising?)
note wp_postmeta is the table name and meta_value is the column name
SET #search = 'View Map +';
SET #replace = 'View New Map2 +';
UPDATE wp_postmeta SET meta_value=REPLACE(meta_value, CONCAT('s:',
LENGTH(#search), ':"', #search, '"'), CONCAT('s:', LENGTH(#replace), ':"',
#replace, '"')) WHERE `meta_id` = 170442
I am getting this error
MySQL returned an empty result set (i.e. zero rows)
I have also tried this second option but it still fails
$old = 'View Map +';
$new = 'View New Map2 +';
$search = 's:' . strlen($old) .':"' . $old . '"';
$replace = 's:' . strlen($new) .':"' . $new . '"';
$query = "UPDATE wp_postmeta SET meta_value=REPLACE(meta_value,
'{$search}','{$replace}') WHERE `meta_id` = 170442 and
meta_value LIKE '%View Map +%';";
I am getting this error
$old = 'View Map +';
MySQL said:
Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '$old =
'View Map +''at line 1
Any ideas?
Thank you

I was scratching my head over this but the above option 1 works fine now.. I did not have the whole array in the search and replace box but part of it thus the error

Related

SELECT part of string between symbol and space

I need to create a subquery (or view) column with values pulled from part of a long string. Values will appear like this:
"Recruiter: Recruiter Name Date:..."
I need to select the recruiter name after : and end with the space after the recruiter name. I understand that normalizing would be better, but we only have query access not database setup access in this case.
Ideas appreciated!
You can use a regex for this. A regex will let you express that you want to search for the text Recruiter followed by a colon, a space, and a series of characters followed by a space, and that you want it to extract those characters.
The expression might look a bit like this (untested)
Recruiter: (.+) Date:
This would look for 'Recruiter: ' literally, followed by a string of any characters (.) of length 1 or larger (+), which is to be extracted (the brackets), followed by the literal string ' Date:'.
How you use this with SQL depends on your vendor.
I would create a function that pulls out the value for a given key. You would use it like:
select [dbo].[GetValue]('recruiter',
'aKey: the a value Recruiter: James Bond cKey: the c value')
This returns 'James Bond'
Here is the function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[GetValue](#Key varchar(50), #Line varchar(max))
returns varchar(max)
as
begin
declare #posStart int, #posEnd int
select #posStart=charindex(#Key, #Line) -- the start of the key
if(#posStart = 0)
return '' -- key not found
set #posStart = #posStart + len(#Key) + 1 -- the start of the value
select #line = substring(#line, #posStart, 1000) -- start #Line at the value
select #posEnd=charindex(':', #line) -- find the next key
if(#posEnd > 0)
begin
-- shorten #line to next ":"
select #line = substring(#line, 0, #posEnd)
-- take off everything after the value
select #posEnd = charindex(' ', reverse(#line));
if(#posEnd > 0)
select #line = substring(#line, 0, len(#line) - #posEnd + 1)
end
return rtrim(ltrim(#line))
end
go

How to delete partial text from SQL table?

A cracker has cracked my SQL database installing the following script into most of the lines:
<script src=http://www.example.com/xx.js></script>
I need to delete in every line just this script, (not all the text in the lines) I have found the command:
DELETE FROM [tevalifeForum].[dbo].[FORUM_MEMBERS]
WHERE <Search Conditions,,>
GO
But I don't know how to proceed.
Assuming your column type is TEXT and you want to delete lines within that TEXT column, here's something similar to what I've done in the past:
DECLARE #script VARCHAR(100), #len INT, #i INT
SELECT
#script = '<script src=http://www.example.com/xx.js></script>',
#len = LEN(#script)
SELECT #i = MAX(DATALENGTH([col]))
FROM [tevalifeForum].[dbo].[FORUM_MEMBERS]
UPDATE [tevalifeForum].[dbo].[FORUM_MEMBERS]
SET [col] =
LEFT(CAST([col] AS VARCHAR(MAX)), CHARINDEX(#script, [col])-1)
+ SUBSTRING(CAST([col] AS VARCHAR(MAX)), CHARINDEX(#script, [col])+ #len, #1)
WHERE [col] LIKE '%' + #script + '%'
Please make sure that you have a valid backup and understand that this can only be 'undone' by restoring from backup.

sql multiple concatenation in a string

I have a big problem that I don't understand.
I have this query in Tsql :
SET #sSQL = 'SELECT *
INTO '+#sTableValeursDefaut+'
FROM OPENQUERY(LINKSVR_LOCAL , '' SET FMTONLY OFF
EXEC [AIGP].[dbo].[rp_WEB_ValeursDefaut_Get]
'''''+'sds'+''''',
'+ISNULL(CONVERT(VARCHAR, #fkIDProjet), 'NULL+')+',
'+'123'+''')'
EXEC(#sSQL)
This work perfectly. But when I change the '123' for CONVERT(VARCHAR, #fkIDCfgFormulaire) and the 'sds' to #sNoUsager the query don't work !
SET #sSQL = 'SELECT *
INTO '+#sTableValeursDefaut+'
FROM OPENQUERY(LINKSVR_LOCAL , '' SET FMTONLY OFF
EXEC [AIGP].[dbo].[rp_WEB_ValeursDefaut_Get]
'''''+#sNoUsager+''''',
'+ISNULL(CONVERT(VARCHAR, #fkIDProjet), 'NULL+')+',
'+CONVERT(VARCHAR, #fkIDCfgFormulaire)+''')'
EXEC(#sSQL)
What I am doing wrong ?
Without more information it's difficult to tell. Could be anything from the datatypes of the variables to the value of the variables you are passing...
We could do with some test data please.
It may also be that you have a '+' character in your literal I haven't tested it but 'NULL+' should probably read 'NULL'
Or it could be because in your CONVERT(VARCHAR, #fkIDProjet) you don't give a length to your varchar try something like this instead CONVERT(VARCHAR(5), #fkIDProjet)

Check if a sentence is in a string in pl/pgsql

I have a pl/pgsql script that needs to check if a word/sentence is in a string, and it must take care of word boundaries, and case insenstive.
Example:
String: "my label xx zz yy", Pattern: "my label", MATCH
String: "xx my label zz", Pattern: "my label", MATCH
String: "my labelxx zz", Pattern: "my label", NO MATCH
So the obvious solution is to use a regex, like this:
select _label ~* (E'\\y' || _pattern || E'\\y') into _match;
It works but is slow, compared to a simple
select _label ilike '%' || _pattern || '%' into _match;
This is wrapped in a function that my script calls A LOT (in the tens of millions, I do a lot of recursion), and with this requirement the overall runtime doubled.
Now my question is, is there a faster way to implement this ?
Thanks.
EDIT: ended up using this:
if _label ilike '%' || _pattern || '%' then
select _label ~* (E'\\m' || _pattern || E'\\M') into _match;
end if;
and it is significantly faster.
I would consider the full text search capabilities, but from what you're describing, I'd likely implement this using PostgreSQL arrays.
First: define a function that takes a label, lowercases it (or uppercase if you prefer), splits it on word boundaries, and returns an array. Say:
CREATE OR REPLACE FUNCTION label_to_array(text) RETURNS text[] AS $$
SELECT regexp_split_to_array(lower($1), E'\\W');
$$ LANGUAGE sql IMMUTABLE;
$ select label_to_array('my label xx zz yy');
label_to_array
---------------------
{my,label,xx,zz,yy}
Now, create a GIN index over this function:
CREATE INDEX sometable_label_array_key ON sometable
USING GIN((label_to_array(label));
From here, PostgreSQL can use this index for many queries involving array operators, such as "contains":
SELECT *
FROM sometable
WHERE label_to_array(label) #> label_to_array('my label');
This query would split 'my label' into {my,label}, and would then use the index to find a list of rows containing my, intersect that with the list of rows containing label, and then return the result. This isn't exactly equivalent to your original query (since it doesn't check their order), but since it uses an index to eliminate most of the rows in the table, adding the original check on the end would work just fine:
SELECT *
FROM sometable
WHERE label_to_array(label) <# label_to_array('my label')
AND label ~* (E'\\y' || 'my label' || E'\\y');

Find a string using PHPMyAdmin

i have table in DB = dle_post and a row contains id,full_story i want to check if full_story starts with "1." then list its id but the big problem is there are some spaces in the start of full_story some time 1 some time 2 and some time 3 , how can i list all ids starting with "1."
You want to execute some SQL like this, which you can also do in PHPmyAdmin...
SELECT id FROM dle_post WHERE LTRIM(full_story) LIKE '1%';
I think this will work!
Would this query help:
$id = fetch id here;
mysql_query("SELECT * FROM YOUR_TABLE WHERE id LIKE '%".$id."`%'", $someconnection);
YOUR_TABLE -> replace it with your table nime

Resources